socket with the WebSocket() constructor:
var s = new WebSocket("ws://ws.example.com/resource");
The argument to the WebSocket() constructor is a URL that uses
the ws:// protocol (or wss:// for a secure connection like that
used by https://). The URL specifies the host to connect to,
and may also specify a port (WebSockets use the same default
ports as HTTP and HTTPS) and a path or resource.
Once you have created a socket, you generally register event
handlers on it:
s.onopen = function(e) { /* The socket is open. */ };
s.onclose = function(e) { /* The socket closed. */ };
s.onerror = function(e) { /* Something went wrong! */ };
s.onmessage = function(e) {
var m = e.data; /* The server sent a message. */
};
In order to send data to the server over the socket, you call the
send() method of the socket:
s.send("Hello, server!");
When your code is done communicating with the server, you
can close a WebSocket by calling its close() method.
WebSocket communication is completely bidirectional. Once
a WebSocket connection has been established, the client and
server can send messages to each other at any time, and that
communication does not have to take the form of requests and
responses.
var s = new WebSocket("ws://ws.example.com/resource");
The argument to the WebSocket() constructor is a URL that uses
the ws:// protocol (or wss:// for a secure connection like that
used by https://). The URL specifies the host to connect to,
and may also specify a port (WebSockets use the same default
ports as HTTP and HTTPS) and a path or resource.
Once you have created a socket, you generally register event
handlers on it:
s.onopen = function(e) { /* The socket is open. */ };
s.onclose = function(e) { /* The socket closed. */ };
s.onerror = function(e) { /* Something went wrong! */ };
s.onmessage = function(e) {
var m = e.data; /* The server sent a message. */
};
In order to send data to the server over the socket, you call the
send() method of the socket:
s.send("Hello, server!");
When your code is done communicating with the server, you
can close a WebSocket by calling its close() method.
WebSocket communication is completely bidirectional. Once
a WebSocket connection has been established, the client and
server can send messages to each other at any time, and that
communication does not have to take the form of requests and
responses.