jiyinyiyong
jiyinyiyong

Reputation: 4713

Why did I failed to connect to websocket?

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?

Upvotes: 0

Views: 5894

Answers (1)

Vadim Baryshev
Vadim Baryshev

Reputation: 26219

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'

Upvotes: 2

Related Questions