Angular Universal server rendering WebSocket

Are there any examples of Angular Universal with WebSockets?

Serverside rendering does not know wat a WebSocket object is in my case. And if I use socket.io the node server hangs when trying to make a connections.

Some additional information about the problem:

I downloaded angular-universal-starter from github: https://github.com/angular/universal-starter Which works fine out of the box running 'npm install' and 'npm start'

But after i added the following code to AppComponent

   export class AppComponent implements OnInit {
       ngOnInit() {
           let webSocket = new WebSocket("----server url----")
       }
   }

I got the following error in my NodeJs server console:

EXCEPTION: WebSocket is not defined
ORIGINAL STACKTRACE:
ReferenceError: WebSocket is not defined
    at AppComponent.ngOnInit (/Volumes/Development/cacadu/website/universal-starter-master2/dist
/server/index.js:41725:29)

Try only calling the websocket on the Client, for example you can detect whether it's the browser or the server with these imports

import { isBrowser, isNode } from 'angular2-universal';

Then use them inside your code, this might be able to fix the problem!

export class AppComponent implements OnInit {
    ngOnInit() {
        if (isBrowser) {
            let webSocket = new WebSocket("----server url----");
        }
    }
}

Some additional information about the problem:

I downloaded angular-universal-starter from github: https://github.com/angular/universal-starter Which works fine out of the box running 'npm install' and 'npm start'

But after i added the following code to AppComponent

   export class AppComponent implements OnInit {
       ngOnInit() {
           let webSocket = new WebSocket("----server url----")
       }
   }

I got the following error in my NodeJs server console:

EXCEPTION: WebSocket is not defined
ORIGINAL STACKTRACE:
ReferenceError: WebSocket is not defined
    at AppComponent.ngOnInit (/Volumes/Development/cacadu/website/universal-starter-master2/dist
/server/index.js:41725:29)
链接地址: http://www.djcxy.com/p/5294.html

上一篇: 如何在Angular Universal项目中添加服务器渲染

下一篇: Angular通用服务器呈现WebSocket