WebSockets is a full-duplex, two-way communication channel with the server, it does not use the HTTP protocol, he has his own protocol that is the custom protocol, the WS protocol, its security protocol for the WSS Protocol. This protocol is specifically designed for fast transmission of small data. There is a requirement for the service that the server must support the protocol, that is, a specific server is required to work.
Websockets API:
(1) First instantiate an object: the var socket = new WebSocket (URL) is followed by the URL that needs to be connected;
(2) There are four states: Socket.readystate= websocket.opening (0) establishing a connection
=websocket.open (1) A connection has been established
=websocket.closing (2) Closing the connection
=websocket.close (3) Closed connection
(3) method of the object: Socket.send (String) string is the passed-in data, and is a string format
Socket.close () Close the connection
(4) Event:
Socket.onmessage=function (event) {
var data =event.data;
}
When the server sends a message to the client, it triggers the messages event, where the data is stored in the Event.data property and the returned data is in string format.
Other events: Socket.onopen;socket.onclose;socket.onerror;
About WebSockets Issues: