0

wsモジュールを使用した Node サーバーのコード:

show = console.log

WS = require('ws').Server
wss = new WS port: 8080
wss.on 'connection', (ws) ->
  show 'a connection'
  ws.on 'message', (message) ->
    show 'received: %s', message
  ws.send 'something'

同じサーバーで以下のコードを実行することで websocket に接続できます"a connection"

WS = require 'ws'
ws = new WS 'ws://localhost:8080'

しかし、ブラウザからこれらのコード行を実行すると、応答がありません:

s = new WebSocket('ws://184.82.253.196:8080');
s.send('nothing');

何が問題なのですか、どうすれば修正できますか?

4

1 に答える 1

2

Websocket サーバーは127.0.0.1、デフォルトでリッスンのみを開始します。このコードを使用して、すべてのインターフェイスのリッスンを開始します。

show = console.log

WS = require('ws').Server
wss = new WS port: 8080, host: '0.0.0.0'
wss.on 'connection', (ws) ->
  show 'a connection'
  ws.on 'message', (message) ->
    show 'received: %s', message
  ws.send 'something'
于 2012-08-23T02:58:19.963 に答える