简体   繁体   English

如何从Websocket连接中挑选特定的服务器消息?

[英]How can I pick out a particular server message from a websocket connection?

I have a WebSocket connection set up for a basic web chat server. 我为基本的Web聊天服务器设置了WebSocket连接。

Now, whenever a message is received, it is sent to a function which outputs it on the screen. 现在,每当接收到一条消息时,它就会被发送到在屏幕上输出消息的功能。

socket.onmessage = function(msg){output(msg);}

However, there are certain technical commands which the user can send to the server through the connection which elicit a technical response, not meant to be output on the screen. 但是,用户可以通过连接将某些技术命令发送到服务器,这会引起技术响应,而不是要在屏幕上输出。

How can I grab the server response which immediately follows one of these technical messages? 如何获取这些技术消息之一之后的服务器响应?

Do I put a separate socket.onmessage right after the block of code which sends the technical message? 我是否在发送技术消息的代码块之后放置一个单独的socket.onmessage? I imagine that would take all future messages. 我想这会带走所有未来的信息。 I just want the next one . 我只想要下一个

Ideas? 有想法吗?

WebSockets is asynchronous so trying to get the 'next' message received is not the right solution. WebSockets是异步的,因此尝试获取收到的“下一个”消息不是正确的解决方案。 There can be multiple message in flight in both directions at the same time. 双向可能同时存在多个消息。 Also, most actions in Javascript are triggered by asynchronous events (timeout firing, or user clicking on something) which means you don't have synchronous control over when sends will happen. 而且,Javascript中的大多数操作都是由异步事件(超时触发或用户单击某些内容)触发的,这意味着您无法同步控制何时发生发送。 Also, the onmessage handler is a persistent setting: once it is set it receives all messages until it is unset. 同样,onmessage处理程序是一个持久性设置:设置后,它将接收所有消息,直到被取消设置。 You need to have some sort of way of distinguishing control messages from data messages in the message it self. 您需要某种方式将自身的消息中的控制消息与数据消息区分开。 And if you need to correlate responses with sent messages then you will also need some kind of message sequence number (or other form of unique message ID). 而且,如果您需要将响应与已发送的消息相关联,那么您还需要某种消息序列号(或其他形式的唯一消息ID)。

For example, this sends a control message to the server and has a message handler which can distinguish between control and message and other messages: 例如,这将控制消息发送到服务器,并具有消息处理程序,该消息处理程序可以区分控制消息和其他消息:

var nextSeqNum = 0;
...
msg = {id: nextSeqNum, mtype: "control", data: "some data"};
waitForMsg = nextSeqNum;
nextSeqNum += 1;
ws.send(JSON.stringify(msg));
...
ws.onmessage = function (e) {
    msg = JSON.parse(e.data);
    if (msg.mtype === "control") {
        if (msg.id === waitForMsg) {
            // We got a response to our message
        } else {
            // We got an async control message from the server
        }
    } else {
        output(msg.data);
    }
};

You can packetise the data, I mean, by a special character/s, form a string like this : 您可以打包数据,我的意思是,用特殊字符组成这样的字符串:

"DataNotToBeShown"+"$$"+"DataToBeShown"; //if $$ is separating character

And then, you can split the string in javascript like this : 然后,您可以像这样在javascript中拆分字符串:

var recv=msg.data.split('$$');

So, the data not be shown is in recv[0] and data to be shown, in recv[1] . 因此,未显示的数据在recv[0] ,而待显示的数据在recv[1] Then use however you want. 然后使用您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM