Article From:https://segmentfault.com/q/1010000010620489
Question:

jsWebsocket API is very simple
conn.send(msg);
What if this msg is a very long text?
When the server reads it, there should be a buffer.
The following library gives demo long text a problem (chat example).

gorilla/websocket

How do we divide long text (or big data)?

Answer 0:

websocket The upper limit of the frame length in the protocol is 2^64, which can be considered without restriction. Therefore, in practice, the length of websocket messages depends only on server implementation.

If the data sent exceeds the server limit, the following options can be considered:

1、Pre sent compression data
Because text data, compression can significantly reduce the volume.

2、Segmented transmission
In general, we will add a custom protocol on the basis of websocket protocol. Do the logic of subcontracting.
Since websocket is based on TCP, it can ensure that packets are in sequence. The implementation of the subpackage is not too difficult. Small or testing systems can do this.
PS: websocket The protocol itself has continued frames and ending frames for subcontracting, but browsers do not provide such interfaces.

3、Modify the server side configuration to increase the limit
Generally speaking, text does not encounter the problem of length restriction. For example, QQ and Wechat only limit the length of a single message, and not through technical means to allow users to send unlimited length of the message. Server support 64K length is no problem, it is also enough.

4、Uploading through other protocols
If you do need to transfer a long text (such as a novel, a document), you should use a separate file upload service (usually based on HTTP). Then only the address uploaded by the file is transferred in websocket. Mature systems generally choose this option and are also easy to choose.Extension (custom expression, short video and so on). But the system is relatively complex, and the cost of development is slightly higher.

Answer 1:

One idea is to divide the files on the customer service side. Webuploader is the way to upload large files.

Answer 2:

Thank you, WebSocket. I stayed at the stage of understanding theory, and I didn’t actually operate it.

Here is a description of the length restriction problem, probably from Get to Post.
http://bbs.csdn.net/topics/39…

In addition, I think we can consider using ready made Websocket components, such as socket.io. General components can also provide compatible solutions to environments that do not support WebSocket.

Answer 3:

There is a problem that doesn’t actually deal with too much data, but one friend of the game has discussed the problem of large data packets, and they are also subcontracting, either post or websocket, but it needs to be noted.ReceiveThe Party of the packet should prevent the disorder, because the network transmission speed is unstable, the B packet sent after the first a packet may be sent, and the recipient may receive the a package after the B packet is received first.

Answer 4:

What if this msg is a very long text?
When the server reads it, there should be a buffer.


Have you guessed yourself or did you try it yourself?
Try to get the problem outmsg It’s much more effective to have discussions on the basis that others can reproduce problems.

Answer 5:

webThe browser itself has made a further frame format encapsulation on the websocket itself. The maximum length of the data section can be 64 bits of integer length, so it is considered that the packet length is not limited.
How the server treats this big packet reading, you want to see the server program, you can choose the flow read, first write the hard disk cache, and then file to the subsequent business processing. This will only waste the hard disk, not to burst memory.

Frame format can take a look at this introduction: Websocket protocol parsing

As a supplementary explanation, the HTTP server handles websocket, which only handles the handshake of the HTTP and returns to the upgrade after the return of the client and the server’s direct Socket connection data, which is as different as the TCP connection.But ordinary browsers have made a frame protocol by themselves.

Link of this Article: Websocket long text problem?

One Reply to “Websocket long text problem?”

Leave a Reply

Your email address will not be published. Required fields are marked *