0

ws带模块的节点服务器代码:

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 回答