Why did I failed to connect to websocket?

2019-08-22 17:08发布

The code of Node server with ws module:

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'

I can connect to websocket by running the code below on the same server, and it gives out "a connection":

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

But when I run these lines of code from browser, there's no respose:

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

What's problem, how can I fix it?

1条回答
爷、活的狠高调
2楼-- · 2019-08-22 17:45

Websocket server start listening only 127.0.0.1 by default. Use this code to start listen all interfaces.

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'
查看更多
登录 后发表回答