为什么我无法连接到websocket? [英] Why did I failed to connect to websocket?

查看:504
本文介绍了为什么我无法连接到websocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

我可以通过在同一台服务器
上运行以下代码来连接到websocket,它发出连接

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?

推荐答案

Websocket服务器默认开始仅监听 127.0.0.1 。使用此代码开始监听所有接口。

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'

这篇关于为什么我无法连接到websocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆