0

I am trying to establish socket communication in chrome extension. I have my extension which would be communicating with "Simple Websocket Client" provided by chrome. But I am not able to establish connection successfully using "WebSocket" class.

I tried using Socket class but later found that it is not supported for extensions. Hence I tried websocket but I am getting error as "Error during websocket handshake : ERR_INVALID_HTTP_RESPONSE."

var wsoc;
wsoc = new WebSocket("ws://localhost:8001");
wsoc.onopen = function()
{
    wsoc.send("Message from chrome extension");
};

I want to see connection successfully established via below client provided by Chrome : Simple WebSocket Client Extension ( https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en )

suprit
  • 79
  • 7

2 Answers2

0

You need to set up a WebSocket server. One could do this with ws, a Nodejs WebSocket library. What you are trying to do is connecting a client to a client, which can't work. You can spin up a WebSocket server as man in the middle between clients. Creating a server in the browser is AFAIK not possible. source

Quisse
  • 646
  • 1
  • 6
  • 24
  • Hi, so I tried the same with by creating server using netcat with command "nc.exe -l -p 8001" and I still am facing the same problem. Getting ERR_INVALID_HTTP_RESPONSE as error. – suprit Oct 01 '19 at 08:50
  • Probably you need something which handles the handshake. More info here https://ssklogs.blogspot.com/2012/10/websockets-handshake-using-netcatbash.html IMHO you'd better use some framework/library which handles this, as mentioned in my answer. – Quisse Oct 01 '19 at 09:08
  • bash script on the given link seems to be crashing. Any idea as to why would that be happening? – suprit Oct 01 '19 at 12:49
  • https://stackoverflow.com/a/57879576/3360802 TL;DR use a 3rd party WebSocket server as mentioned. – Quisse Oct 01 '19 at 12:58