Home  >  Article  >  Backend Development  >  If you want to use websocket to send pictures and videos, how does the client distinguish between different data (text, pictures...)?

If you want to use websocket to send pictures and videos, how does the client distinguish between different data (text, pictures...)?

WBOY
WBOYOriginal
2016-08-18 09:16:032412browse

JS operation is like this

<code>ws = new WebSocket($("#uri").val());
    //连接成功建立后响应
        ws.onopen = function() {
        }
    //收到服务器消息后响应
        ws.onmessage = function(e) {
        }
        //连接关闭后响应
        ws.onclose = function() {
            ws = null;
        }</code>

Is there any js interface

Reply content:

JS operation is like this

<code>ws = new WebSocket($("#uri").val());
    //连接成功建立后响应
        ws.onopen = function() {
        }
    //收到服务器消息后响应
        ws.onmessage = function(e) {
        }
        //连接关闭后响应
        ws.onclose = function() {
            ws = null;
        }</code>

Is there any js interface

Websocket is just a long connection communication protocol and does not provide actual business functions.
The transmitted content requires the communicating parties (front-end and back-end) to negotiate a message format that specifies how to obtain the type, content and other fields. Once agreed, data encapsulation and parsing are carried out accordingly.

Generally speaking, websocket is suitable for transmitting text data. Although the protocol also stipulates that binary data can be transmitted, JS is really not good at handling binary data. Therefore, it is better to encode the binary data into text data. There are many methods, such as base64, etc., and then send it over. The answer on the 1st floor is also very good and explained very clearly. I think the original poster is not very clear about protocols and the like. I suggest you read more books in this area. It is very useful for improving your programming level.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn