简体   繁体   English

如何使用nodejs中的ws npm模块从客户端向WebSocket服务器发送“升级”握手?

[英]How to send “Upgrade” handshake to WebSocket server from client using ws npm module in nodejs?

I am using ws npm module to build my WebSocket client and server in nodejs 我正在使用ws npm模块在nodejs中构建我的WebSocket客户端和服务器

From client I have to send a "Upgrade" handshake request to the server with some specified protocols to the server. 从客户端,我必须向服务器发送“升级”握手请求,并向服务器发送一些指定的协议。

From the client side, this is what I use to make a conenction to the server. 从客户端来看,这就是我用来连接服务器的方法。

const ws = new WebSocket('ws://localhost:8989/');
ws.on('open', function open() {
          console.log('Connected to Server');
          })

And this is my handshake request, 这是我的握手请求,

GET <target> HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: com.xxx.service.v1
Sec-WebSocket-Version: 13

How should I add Upgrade headers and protocols to my client side connection request using ws npm module and how should I interpret them in server side? 如何使用ws npm模块将升级标头和协议添加到客户端连接请求中,以及如何在服务器端解释它们?

This is my sample server side code, 这是我的示例服务器端代码,

var port: number = 8989;
var WebSocketServer = WebSocket.Server;
var wss = new WebSocketServer({ port: port });

wss.on('connection', (ws: WebSocket) => {
    console.log('Web Socket Server Connected');
});

I cant figure how to send these these Upgrade headers via ws client and how to interpret them in the server side? 我无法弄清楚如何通过ws客户端发送这些升级头以及如何在服务器端解释它们?

After this handshake, I have to send a preamble via websocket text message. 握手之后,我必须通过websocket文本消息发送一个序言。

Please suggest. 请提出建议。

Thanks 谢谢

Some web services like heroku handle this for you, but others like openshift doesn't it. 一些网络服务(如heroku)可以为您处理此问题,但其他服务(如openshift)则不是。 Just try 'wss' intead of 'ws in the url string. 只需在url字符串中尝试'ws'的'wss'intead。

If you want to do this on localhost try to use https module instead of http. 如果要在本地主机上执行此操作,请尝试使用https模块而不是http。 And if you need to do it manualy, try setting the headers when creating the websocket 并且,如果您需要手动进行操作,请在创建WebSocket时尝试设置标题

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

Where you setting the port you can set the headers too, but you must to know the correct fields to set for the browser to accept them. 在设置端口的地方也可以设置标头,但是必须知道正确的字段才能为浏览器接受。

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

相关问题 NodeJS 作为 Websocket 客户端使用 ws - NodeJS as Websocket client using ws 如何使用websocket将json中的base64 url​​数据从javascript客户端发送到nodejs服务器? - How to send base64 url data using json from javascript client to nodejs server using websocket? 如何在没有客户端请求Node.js中的ws服务器的情况下将数据发送到客户端 - How to send Data to the client with out client requesting the ws server in nodejs 如何从 nodejs 服务器订阅 ws websocket 到另一台服务器? - How can subscribe to ws websocket from a nodejs server to another server? 使用node.js websocket库ws时如何知道客户端与服务器断开连接 - how to know the client being disconnected from the server when using node.js websocket library ws npm&#39;websocket&#39;服务器向客户端发送消息 - npm 'websocket' server send message to client Node.js WebSocket:如何在没有服务器响应的情况下连续发送客户端消息 - Nodejs websocket: how to continuously send client message without server response 如何使用Express(NodeJS)从服务器向客户端发送大量文件 - How to send a lot of files from server to client using express (NodeJS) 使用json和ws将文本从客户端发送到服务器 - Send text from client to server using json and ws 使用nodejs向特定的websocket客户端发送消息 - Send message to specific websocket client using nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM