porsager/pws

Chrome issue - connects with null protocol header

exabrial opened this issue · 17 comments

Is there a way to use this without node?

Do you mean in the browser?

The first usage example is in the browser.

You can use
https://unpkg.com/pws
or
https://unpkg.com/pws?module

Let me know or reopen if this wasn't the answer you looked for ;) I'll close this for now.

Apologies.

So lets say you have an app that doesn't use node or a package manager. Is using PWS as simple as including the index.js you linked above?

Ah yes, that's correct ;)

So you can do

<script src="pws.js"></script>
<script>
  const socket = new PersistentWebSocket(url)
  // More code here
</script>

Here's a simple sample

Awesome thanks.

We're doing the following:

var	websocket;
function connectSocket() {
	var wsUrl = 'ws://localhost:8080/something';
	websocket = new PersistentWebSocket(wsUrl, null, {
		reachabilityTestUrl: "/haproxy.jsp" 
	});
	window.onbeforeunload = function() {
	    websocket.onclose = function() {};
	    websocket.close();
	};
	websocket.onmessage = (event) => {
		if (event.data === 'update') {
			updateTexts();
		} else if (event.data === 'ping?') {
			websocket.send('pong!');
		} else if (event.data === 'optout') {
			forceOptout = true;
			receivedOptInOutStatus = true;
			enableDisableOptOutStatusIndicator();
		} else if (event.data === 'optin') {
			forceOptout = false;
			receivedOptInOutStatus = true;
			enableDisableOptOutStatusIndicator();
		}
	};
	websocket.open();
}

And getting this error in the console:
WebSocket connection to 'ws://localhost:8080/something' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

Yes, browsers set the header by default unless you specify one, so your server should handle that. This is not really related to pws, but websockets In general.

interesting, why does PWS fail? If we use plain websocket it doesn't do this. Is there a way to disable that behavior?

Does it work if you don't supply the extra arguments to pws?

Just give it the URL and nothing else

I did, same problem! But changing

websocket = new PersistentWebSocket(wsUrl);

to

websocket = new WebSocket(wsUrl);

Works! Could it be related to TLS somehow? I notice all of the examples are using it, whereas locally we don't have it running

Actually check this out, this is with PWS:

Screen Shot 2019-08-30 at 2 20 40 PM

this is with plain websocket:
Untitled

Ah right, you might be on to something! Let me run a quick test

Ahha! Chrome changed the behavior of new WebSocket! :-(

I'll make a fix immidiately

Thanks a lot for the report :) It's fixed in v4.1.1, just released.

Hah you bet! thank YOU for helping me out