Unable to open web connector

I tried to write a piece of code to open a web socket.

var ws = null; var close = function() { if (ws) { console.log('closing ...'); ws.close(); } } var onOpen = function() { console.log('opening...'); ws.send('hello. im connected'); }; var onClose = function() { console.log('closed'); }; var onMessage = function(event) { var data = event.data; console.log('message: ' + data); }; close(); var url = 'ws://localhost:9999'; ws = new WebSocket(url); ws.onopen = onOpen; ws.onclose = onClose; ws.onmessage = onMessage; console.log('ws: ' + ws); if (ws) { console.log('url:' + ws.url); console.log('readyState:' + ws.readyState); ws.send(msg); } else alert('no ws'); 

But, seeing the console, readyState continued to CONNECT, which throws an exception to the socket send () command.

I tried the extension https://chrome.google.com/extensions/detail/pfdhoblngboilpfeibdedpjgfnlcodoo and got the same problem. The server is pretty simple, which takes an incoming socket and writes it to the console.

Initially, a hand shake, but the readyState client was still persisting 0.

Here is a hand shake:

 GET / HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: localhost:9999 Origin: http://localhost:8080 Sec-WebSocket-Key1: Qb 15 05 6 Gq 9 26 u0 6 Sec-WebSocket-Key2: 8096 C0587|7. 

Do I have something specific on the server to open the client?

Thanks guys.

+4
source share
2 answers

Your handshake block is incomplete. There should be an empty string followed by 8 bytes of additional data (or key3).

The server needs to create a reverse handshake that looks something like this:

 HTTP/1.1 101 Web Socket Protocol Handshake\r Upgrade: WebSocket\r Connection: Upgrade\r Sec-WebSocket-Origin: http://localhost:8080\r Sec-WebSocket-Location: ws://localhost:9999/\r Sec-WebSocket-Protocol: sample\r \r [16 byte md5 hash] 

The 16-byte hash returned by the server is calculated as follows:

  • take numbers from key1 data and convert them to a number. Divide this number by the number of data spaces key1. This gives you a 4 byte key1.

  • do the same with the key2 data to get the key2 number.

  • create an array of 16 bytes, packs of key1 (4 bytes), followed by key2 (4 bytes), followed by 8 bytes of additional data received from the client (8 bytes).

  • md5 sum a 16 byte array to get a new 16 byte array. This is what the client wrote to complete the handshake response.

Until the server returns the handshake, the client will display “CONNECTION”.

The response to connecting to the server is described in more detail in section 5.2 of WebSockets version 76

Update on the second question (in the answer section):

If all you are trying to do is send data from the extension, then you can simply use the XMLHttpRequest POST request (AJAX). Since you are getting something from the request, I suspect that latency is not so important for your application. XMLHttpRequest should easily support extensions for both Firefox and Chrome.

On the other hand, if latency is important or if your server where the data is sent is exclusively a socket server, adding WebSockets support will not be much more complicated than adding HTTP support (in fact, it might be easier). WebSockets support should also support extensions in both Chrome and firefox (4.0 onwards).

+1
source

Thanks.

I did not know that these are 2 ways of communication.

I encoded a Chrome extension that opens a socket and sends data to a specific application. I did this with FF using the socket below:

 var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService); var transport = transportService.createTransport(null,0,"localhost",9999,null); 

Is there a way that uses a similar approach in Chrome? Or should I use WebSocket with two communication methods? I do not want this to be difficult, as I need to modify the recipient application in order to return the handshake.

Thanks.

0
source

All Articles