带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');
问题是什么,我该如何解决?
答案 0 :(得分:1)
默认情况下,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'