Hu: Besides payload delivery, this title is the best way to describe what needs to happen # between two | parties, which we can call client-server, or the requester-acceptor. This process includes the building and delivery of the HTTP-socket, but this may not be the only step, depending on the extent to which # we want to rely on the JS.built-in,lib:
H3S1: JavaScript’s built.in-Web,Sockets.lib, and testing#<lit-rev>:
H4S1: Creating a Websocket object:
dev-Mozilla: In order to communicate using the WebSocket | protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The WebSocket constructor accepts one required and one optional parameter:
webSocket = new WebSocket(url, protocols);
Hu: Is all you need, to establish a formal “Web.Socket-conn”, 2 files, one that can send the client.HTTP-req, and one that can send back the server.HTTP-req? dev-Mozilla: url
: The URL to which to connect; this should be the URL to which the WebSocket server will respond. This should use the URL scheme wss://
, although some software may allow you to use the insecure ws://
for local connections. Hu: The protocols argument, for sub–protocols<un-intuitive!>, is optional, and we’ll start without it for now. dev-Mozilla: The constructor will throw a SecurityError
<built-in!> if the destination doesn’t allow access.
H5S1: Conn-errors<built-in!>
If an error occurs while attempting to connect, first a simple event with the name error
is sent to the WebSocket
object (thereby invoking its onerror
handler), and then the CloseEvent
is sent to the WebSocket
object (thereby invoking its onclose
handler) to indicate the reason for the connection’s closing. The browser may also output to its console a more descriptive error message as well as a closing code as defined in RFC 6455, Section 7.4 through the CloseEvent
.
H5S2: JS-arg: Ultimately, we do want the browser to be making the conn, and delivering the obj to the server, so it makes sense to use JS, which has quite a bit of built.in-func, developed since ’11, such as error | handling, and hopefully, some standardization | features.
H5S3: Does JS automatically send the HTTP-request? <dev-Mozilla>: Establishing a WebSocket relies on the HTTP Upgrade mechanism, so the request for the protocol upgrade is implicit when we address the web server as ws://www.example.com
or wss://www.example.com
.
H4S2: Checking.conn-status:
H5S1: WebSocket.readyState: <dev-Mozilla>: The WebSocket.readyState
read-only property returns the current state of the WebSocket
connection. Value: One of the following unsigned short
values:
Value | State | Description |
---|---|---|
0 | CONNECTING | Socket has been created. The connection is not yet open. |
1 | OPEN | The connection is open and ready to communicate. |
2 | CLOSING | The connection is in the process of closing. |
3 | CLOSED | The connection is closed or couldn’t be opened. |
H5S1: WebSocket: open event:
<dev-Mozilla>: The open event is fired when a connection with a WebSocket is opened. Syntax: Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener('open', (event) => { })
onopen = (event) => { }
//example:
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', (event) => {
socket.send('Hello Server!');
});
H3S4<fbno.v-t>: Setting the server to listen:
References:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism