【问题标题】:Why did I failed to connect to websocket?为什么我无法连接到 websocket?
【发布时间】:2012-08-23 12:13:02
【问题描述】:

带有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');

什么问题,我该如何解决?

【问题讨论】:

    标签: node.js coffeescript websocket


    【解决方案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'
    

    【讨论】:

      猜你喜欢
      • 2017-07-30
      • 2017-05-01
      • 1970-01-01
      • 2017-06-06
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多