0

I am rather confused with mechanics and hierarchy of things in javascript, reading few links and pages are adding to them.

There are various ways to create objects in Javascript. What I am getting confused is in following:

I have 3 websocket say WSServiceA, WSServiceB, WSServiceC : These are 3 different sockets and provide different data but data structure is same. Using a simple function I am updating 3 tables in web-page.

Each of these have onopen, onclose and onmessage function so I have simply copied same code 3 times, like this:

WSServiceA.onmessage(){}
WSServiceB.onmessage(){}
WSServiceC.onmessage(){}

If I create a something like this:

function CreateService(Service, WSAddress){
      this.service=Service;
      this.WSAddress = WSAddress;
      this.websocket = new WebSocket(this.WSAddres);
      this.onopen = function(event){ Send some message }
      this.onmessage = function(event){ Update Table A}
    }

After that I will create

ServiceA = CreateService("A", "ws://192.168.100.1.1:10001");
ServiceB = CreateService("B", "ws://192.168.100.1.2:10001");
ServiceC = CreateService("C", "ws://192.168.100.1.3:10001");

Then how and who will call ServiceA.onopen, Service.onmessage and other functions - when data comes to those sockets? Will it be called at all? Something created inside function should be visible only in that function - I am not able to understand flow and link. Probably I am missing some key concept or this should not work.

Community
  • 1
  • 1
Aval Sarri
  • 198
  • 2
  • 13
  • in onopen function you should have access to the socket by refering to `this`, if not, you can create a function with `bind` to bind it to an object – Adam Wolski Feb 04 '17 at 10:38
  • Perhaps start with the [*The WebSocket API spec*](https://www.w3.org/TR/websockets/). If you don't understand events, perhaps [*quirksmode: Introduction to Events*](http://www.quirksmode.org/js/introevents.html) will help. – RobG Feb 04 '17 at 11:13
  • these `.on` * are **event listeners** When the respective event will fire, they will be listening for it and will execute the callback function. – candh Feb 04 '17 at 11:34

1 Answers1

0

Background

There is ECMAScript, which is the language, and javascript, which is a generic name for an implementation of ECMAScript for a particular host (noting that JavaScript™ is a trademark of Oracle Corporation).

ECMAScript by itself does very little, it doesn't even have a mechanism for input or output, it must be provided by the host. In regard to WebSockets, if the host supports them then it will implement the WebSockets API.

When you create a WebSocket through new WebSocket(...), then the host returns a host object that implements the interface. The object will have a state that changes in response to its environment (connecting, open, closing, closed) and it will dispatch events when certain things happen.

When the socket successfully makes a connection to the server, it will dispatch an open event on the socket, so that if there is an onopen listener, it will respond to the event by being called by the open handler. There are also events for error and close that have associated on* handlers.

Answer

Then how and who will call ServiceA.onopen, Service.onmessage and other functions - when data comes to those sockets?

The host environment implementing the interface does all that.

RobG
  • 142,382
  • 31
  • 172
  • 209