简体   繁体   English

为什么我在WebSocket握手请求标头中看不到cookie值?

[英]Why can't I see cookie value in a WebSocket handshake request header?

I wanted to send a websocket handshake request from an html page and wrote codes like below: 我想从html页面发送websocket握手请求,并编写如下代码:

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

But what shown in Chrome Dev Tools appears that no cookie was sent: 但Chrome开发工具中显示的内容显示没有发送Cookie:

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 there is no cookie in request headers? 为什么请求标头中没有cookie? If I send other ajax request not upgraded, I can see cookies in Dev Tools. 如果我发送其他未升级的ajax请求,我可以在Dev Tools中看到cookie。 Why is there such differences? 为什么会有这样的差异?

Your Origin header is http://localhost:63342 and the WS request is to ws://localhost:5000/ws so your domains are different (different ports). 您的Origin标头是http:// localhost:63342 ,WS请求是ws:// localhost:5000 / ws所以您的域是不同的(不同的端口)。 I imagine your other ajax requests are to http://localhost:63342 and thus the browser sends the cookie. 我想你的其他ajax请求是http:// localhost:63342 ,因此浏览器发送cookie。

You can try 你可以试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM