Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
Node.js “如何发送”;“升级”;在nodejs中使用ws-npm模块从客户端与WebSocket服务器握手?_Node.js_Websocket_Server_Client_Handshake - Fatal编程技术网

Node.js “如何发送”;“升级”;在nodejs中使用ws-npm模块从客户端与WebSocket服务器握手?

Node.js “如何发送”;“升级”;在nodejs中使用ws-npm模块从客户端与WebSocket服务器握手?,node.js,websocket,server,client,handshake,Node.js,Websocket,Server,Client,Handshake,我正在使用ws-npm模块在nodejs中构建我的WebSocket客户端和服务器 我必须从客户端向服务器发送一个“升级”握手请求,并使用一些指定的协议 const ws = new WebSocket('ws://localhost:8989/'); ws.on('open', function open() { console.log('Connected to Server'); }) 从客户端来看,这是我用来与服务器连接的 const ws =

我正在使用ws-npm模块在nodejs中构建我的WebSocket客户端和服务器

我必须从客户端向服务器发送一个“升级”握手请求,并使用一些指定的协议

const ws = new WebSocket('ws://localhost:8989/');
ws.on('open', function open() {
          console.log('Connected to Server');
          })
从客户端来看,这是我用来与服务器连接的

const ws = new WebSocket('ws://localhost:8989/');
ws.on('open', function open() {
          console.log('Connected to Server');
          })
这是我的握手请求

GET <target> HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: com.xxx.service.v1
Sec-WebSocket-Version: 13
我不知道如何通过ws-client发送这些升级头,以及如何在服务器端解释它们

在这次握手之后,我必须通过websocket文本消息发送序言

请建议


谢谢

像heroku这样的一些web服务会为您处理这个问题,但像openshift这样的其他web服务不会。 只需在url字符串中尝试“wss”intead of“ws”

如果要在本地主机上执行此操作,请尝试使用https模块而不是http。 如果需要手动操作,请在创建websocket时尝试设置标题

    var wss = new WebSocketServer({ port: port,
                                    aField: 'aValue',
                                    anotherField: 'anotherValue' });

在设置端口的地方,您也可以设置标题,但您必须知道要为浏览器设置的正确字段才能接受它们。

一些web服务(如heroku)可以为您处理此问题,而其他web服务(如openshift)则不能。 只需在url字符串中尝试“wss”intead of“ws”

如果要在本地主机上执行此操作,请尝试使用https模块而不是http。 如果需要手动操作,请在创建websocket时尝试设置标题

    var wss = new WebSocketServer({ port: port,
                                    aField: 'aValue',
                                    anotherField: 'anotherValue' });
在设置端口的地方,您也可以设置标题,但您必须知道要设置的正确字段,以便浏览器接受它们