2017-04-12 21 views
0

私は受信データ用にwebsocketを購読する必要のある角度のあるアプリケーションを持っています。typescript websocketでメッセージを送信する方法

import {Server} from "ws"; 
var wsServer: Server = new Server({port: 8085}); 
console.log('ws on 8085'); 
wsServer.on('connection',websocket => websocket.send('first pushed message'));////// 

私の質問:私は

setContextChannel(channel) { 
    this.contextWS = new WebSocket(channel); 
    that = this; 
    this.contextWS.onmessage = function(event) { 
     console.log('ws', event.data); 
     that.patientID = event.data; 
    }; 

    this.contextWS.onerror = function(event) { 
     console.log('ws error', event); 
    } 
} 

を持ち、モックサーバ側で、私は次のようにソケットを作成typescriptです・ノード・サーバーを持っているサービスに角度側では

、 wsServerを使ってメッセージを送信する方法は?

答えて

1

あなたは何について尋ねているのですか?この行は正しいです:

wsServer.on('connection',websocket => websocket.send('first pushed message')); 

あなたが接続されているすべてのクライアントにメッセージを送信しておきたい場合は、あなたが接続されている各クライアントにそれぞれ接続されたクライアントにメッセージを送信するために、プロパティwsServer.clientsを使用するか、または参照を格納するためのいずれかの必要があります(コード内の変数websocket)を配列に格納し、forEach()を使用して各クライアントにメッセージを送信します。

は、このサンプルコードを見てみましょう:私が探していたものhttps://github.com/Farata/angular2typescript/blob/master/chapter8/http_websocket_samples/server/bids/bid-server.ts

+0

感謝はあなたのリンクに記載されています。 websocket.on( 'message'、私が探していたものです – reza

+0

素晴らしいです。 –

関連する問題