Why can't I see the cookie value in the header of the request to confirm the connection to WebSocket?

I wanted to send a websocket confirmation request from the html page and write the codes as shown below:

document.cookie = "guestId=xxxx; remember=xxxxxx;"; var ws = new WebSocket("ws://localhost:5000/ws"); 

But what is shown in Chrome Dev Tools shows that the cookie is not sent:

 General: Request URL:ws://localhost:5000/ws Request Method:GET Status Code:307 Temporary Redirect Response Headers Content-Length:59 Content-Type:text/html; charset=utf-8 Date:Fri, 18 Mar 2016 09:39:11 GMT Location:/preorder/landing/index Request Headers Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4 Cache-Control:no-cache Connection:Upgrade Host:localhost:5000 Origin:http://localhost:63342 Pragma:no-cache Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits Sec-WebSocket-Key:t3N0vVaLCsOmOXLSh+Arsw== Sec-WebSocket-Version:13 Upgrade:websocket User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 

How can i fix this? Why are there no cookies in the request headers? If I send another ajax request not updated, I can see the cookies in Dev Tools. Why are there such differences?

+6
source share
2 answers

The Origin header is http: // localhost: 63342 , and the WS request is ws: // localhost: 5000 / ws so your domains are different (different ports). I believe your other ajax requests are at http: // localhost: 63342 and thus the browser sends a cookie.

+1
source

You can try

 ws.on("headers", function(headers) { headers["set-cookie"] = "yokokkie"; }); 
0
source

All Articles