H2S85: Building, establishing, and maintaining a Web.Sockets-Conn between 2 parties:

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 subprotocols<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:

0CONNECTINGSocket has been created. The connection is not yet open.
1OPENThe connection is open and ready to communicate.
2CLOSINGThe connection is in the process of closing.
3CLOSEDThe 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) => { }
// 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:





Leave a Reply

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