websocket客户端向服务器发送信息,npm'websocket'服务器发送消息给客户端(npm 'websocket' server send message to client)...

npm'websocket'服务器发送消息给客户端(npm 'websocket' server send message to client)

在我继承的代码中使用了这个 websocket。 我阅读了文档,并进行了大量的谷歌搜索以查找websocketServer如何向客户端(浏览器)发送消息。 这是一段代码片段:

var wsServer = new WebSocketServer({

httpServer: server,

autoAcceptConnections: false,

path:"/async" //This attribute is not in the documentation

});

wsServer.on('request', function(request) {

var connection = request.accept('relay_protocol', request.origin);

connection.on('message', function(message) {

....

});

});

我无法找到connection对象的文档。 它有什么好处?

最后,用什么方法将消息发送回客户端?

总的来说,这个模块提供的信息非常差。 请帮忙。

In the code that I inherited is used this websocket. I read the documentation and did a lot of google search to find how websocketServer can emit message to the client(browser). Here is a code snippet:

var wsServer = new WebSocketServer({

httpServer: server,

autoAcceptConnections: false,

path:"/async" //This attribute is not in the documentation

});

wsServer.on('request', function(request) {

var connection = request.accept('relay_protocol', request.origin);

connection.on('message', function(message) {

....

});

});

I wasnt able to find documentation for connection object. What propety does it have?

And last, what method to use to send message back to the client?

In general the information given for this module is very poor. Please help.

原文:https://stackoverflow.com/questions/50354259

更新时间:2020-01-08 22:24

最满意答案

这个模块的完整文档在这里 。

从他们的例子(服务器):

connection.on('message', function(message) {

if (message.type === 'utf8') {

console.log('Received Message: ' + message.utf8Data);

connection.sendUTF(message.utf8Data);

}

else if (message.type === 'binary') {

console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');

connection.sendBytes(message.binaryData);

}

});

所以这两个都会向客户端发送一条消息:

connection.sendUTF(message.utf8Data);

connection.sendBytes(message.binaryData);

我希望这有帮助!

提示:尝试使用socket.io模块。

The full documentation for this module is here.

From their example (server):

connection.on('message', function(message) {

if (message.type === 'utf8') {

console.log('Received Message: ' + message.utf8Data);

connection.sendUTF(message.utf8Data);

}

else if (message.type === 'binary') {

console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');

connection.sendBytes(message.binaryData);

}

});

So these both sends a message to the client:

connection.sendUTF(message.utf8Data);

connection.sendBytes(message.binaryData);

I hope this helps!

Tip: try out the socket.io module.

相关问答

对于126到65536字节之间的消息, pack的格式字符串看起来是错误的。 你能尝试改变吗? “CCS” - 无符号短(总是16位,机器字节顺序) 至 “CCn” - “网络”(big-endian)顺序中的无符号短(16位) The format string for pack looks wrong for messages between 126 and 65536 bytes. Can you try changing "CCS" - unsigned short (always 16

...

您需要决定要触发客户端向服务器发送新数据的内容。 您不应该使用来自服务器的消息的传入到达来触发与传入服务器消息无关的客户端发送。 您触发客户端发送的选项包括: 使用setTimeout()或setInterval()以定期时间间隔(例如每30秒)向服务器发送一些内容。 响应客户端中的某些事件,并将其用作将数据发送到服务器的触发器(例如按钮单击,鼠标移动,数据到达等)。 如果您更具体地了解您尝试发送到服务器的内容以及何时将其发送到服务器,我们可以更具体地建议如何最好地安排或触发发送。 You nee

...

这个模块的完整文档在这里 。 从他们的例子(服务器): connection.on('message', function(message) {

if (message.type === 'utf8') {

console.log('Received Message: ' + message.utf8Data);

connection.sendUTF(message.utf8Data);

}

else if (message.type ===

...

wss.on("connection", function connection(ws) { ws.on('message', function(message) { console.log('received: %s', message); }); ws.send("something"); }); 看看回调on('message',你定义的函数不是一个普通的匿名函数,用作回调也是message而不是message, (注意逗号)。 尝试使用示例回显服务器以确保一切正常,使用iOS客户端,并且您

...

请记住, websocket_client:cast/2的第一个参数必须是websocket_client进程的pid。 你可以从start_link调用中获取pid,例如: {ok, Pid} = websocket_client:start_link("wss://echo.websocket.org", ?MODULE, []).

并将消息转发给远程服务器: websocket_client:cast(Pid, {text, <>}).

在websocket_c

...

好吧,我想我明白了,对于每个需要它的人来说,这就是答案: 首先,您需要将WS依赖项添加到pom.xml

org.springframework.boot

spring-boot-starter-websocket

org.springframework

...

您必须使用连接池将消息广播到所有连接。 您可以将其用作教程/示例http://gary.burd.info/go-websocket-chat 简化: 连接池是已注册连接的集合。 请参阅hub.connections : type connection struct {

// The websocket connection.

ws *websocket.Conn

// Buffered channel of outbound messages.

send cha

...

我解决了这个问题。 它现在有效,因为事实证明在握手结束时需要额外的\ r \ n( https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers )。 所以我的握手是不正确的,但Firefox以某种方式接受它(但不接受消息)。 无论如何,我上面发布的链接表示握手格式应该是: HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Co

...

只有一个客户端收到您的消息的原因是该session变量仅包含发送给您消息的客户端的连接。 要将消息发送到所有客户端,请将它们的连接存储在onOpen()方法的某个集合(例如, ArrayList )中,然后迭代该集合以获取所有客户端的连接 The reason why only one client receives your message is that session variable contains connection only of that client who

...

使用升级连接的HTTP GET请求建立WebSocket连接。 您可以使用(int) $resource将资源转换为整数,从而根据PHP中的资源ID识别客户端。 TCP连接通常由源IP /源端口/目标IP /目标端口四倍标识。 您必须将URI /端点信息保存在数组或类似的数据结构中,并使用客户端ID作为索引。 然后,您可以在收到新消息时查找端点。 A WebSocket connection is established using an HTTP GET request that upgrade

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值