Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中创建websocket时发送自定义标题?_Javascript_Python_Websocket - Fatal编程技术网

如何在javascript中创建websocket时发送自定义标题?

如何在javascript中创建websocket时发送自定义标题?,javascript,python,websocket,Javascript,Python,Websocket,因此,我在Python上编写了以下创建websocket连接的代码: channels_dict = {} channels_dict['Authorization'] = 'true' for channel in channels: #adds some extra headers with info channels_dict["Channel" + str(channel)] = '1' ws = websocket.WebSocketApp("ws://localhost:8

因此,我在Python上编写了以下创建websocket连接的代码:

channels_dict = {}
channels_dict['Authorization'] = 'true'
for channel in channels: #adds some extra headers with info
    channels_dict["Channel" + str(channel)] = '1'

ws = websocket.WebSocketApp("ws://localhost:8888/ws",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close,
                              header = channels_dict)
而且我可以很容易地添加额外的头,我可以在连接后在服务器中访问这些头。有没有一种方法可以在Javascript中实现同样的功能?我还没有找到关于在websocket创建中设置自定义标题的更多信息。我在JS中有以下代码:

var webSocket;

function openWebsocket() {
  webSocket = new WebSocket("ws://localhost:8888/ws")
}
我听说过在url中添加查询参数,这是在javascript中向websocket连接添加额外标题/信息的唯一方法吗?

尝试以下方法:

 const webSocket = new WebSocket ("ws://localhost:8888/ws"  + "?param1=" + param1); // here you can add other params
 webSocket.on("open", function open(e) {
   console.log("*** websocket opened");
 });

好的,现在我这样做了:
webSocket=newwebsocket(“ws://localhost:8888/ws?param1=yooo”)
。我连接的服务器基本上是一个Tornado服务器,但我认为这并不重要。在下一个注释中,我将在服务器接收到websocket连接尝试时,将收到并打印的标题放入服务器。
Host:localhost:8888连接:升级Pragma:no-cache-cache-Control:no-cache-Upgrade:websocket-Origin:chrome-extension://aegbhodkdcknhkiimijaibmipgknpphb Sec Websocket版本:13用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/74.0.3729.169 Safari/537.36接受编码:gzip,deflate,br接受语言:pt-pt,pt;q=0.9,en-US;q=0.8,en;q=0.7秒Websocket键:V7K2QUIifuqI37ao0VZCHA==秒Websocket扩展:permessage deflate;客户端_max_window_bits
我不认为这些url参数被添加到了头中,我要在s中搜索到哪里可以访问它们好的,我想我可以通过我收到的uri在服务器上访问它们。但是这会有点复杂,因为我必须分析uri字符串来搜索参数。之前我只是在headers字典中搜索它们,这很简单。