最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Web Socket connection with Basic Access Authentication - Stack Overflow

programmeradmin2浏览0评论

I'm trying to establish connection (client side) with Web Socket which requires Basic Access Authentication. From documentation I know that I should use username and password with base64 encryption. It's node.js application so I decide to use npm package SockJS-client. It seems to be good solution because:

Under the hood SockJS tries to use native WebSockets first. If that fails it can use a variety of browser-specific transport protocols and presents them through WebSocket-like abstractions.

But I have no clue how to establish connection using Basic Access Authentication. I have tried solution from here, but this one

var ws = new WebSocket("ws://username:[email protected]/service");

does not work, I got an error like:

SyntaxError: The URL's scheme must be either 'http:' or 'https:'. 'ws:' is not allowed.

When I'm trying with http/https I got:

Error: InvalidStateError: The connection has not been established yet

When I'm trying to call

sock.send('test');

Another solutions like that below is impossible to implement because I have no access to server side of Web Socket.

var ws = new WebSocket("ws://example/service?key1=value1&key2=value2");

I have no idea how to correctly establish authenticated connection with Web Socket, if you know any solutions let me know.

I'm trying to establish connection (client side) with Web Socket which requires Basic Access Authentication. From documentation I know that I should use username and password with base64 encryption. It's node.js application so I decide to use npm package SockJS-client. It seems to be good solution because:

Under the hood SockJS tries to use native WebSockets first. If that fails it can use a variety of browser-specific transport protocols and presents them through WebSocket-like abstractions.

But I have no clue how to establish connection using Basic Access Authentication. I have tried solution from here, but this one

var ws = new WebSocket("ws://username:[email protected]/service");

does not work, I got an error like:

SyntaxError: The URL's scheme must be either 'http:' or 'https:'. 'ws:' is not allowed.

When I'm trying with http/https I got:

Error: InvalidStateError: The connection has not been established yet

When I'm trying to call

sock.send('test');

Another solutions like that below is impossible to implement because I have no access to server side of Web Socket.

var ws = new WebSocket("ws://example.com/service?key1=value1&key2=value2");

I have no idea how to correctly establish authenticated connection with Web Socket, if you know any solutions let me know.

Share Improve this question asked Oct 29, 2017 at 9:45 BearBear 1,1071 gold badge10 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 18

Solution was pretty simple, but I think it is worth to place it here for next generations.

Finally, I implemented it by ws, you can find it here. First of all, I added header with field Authorization which contains encoded token. Another thing was a flag perMessageDeflate set to false.

The client will only use the extension if it is supported and enabled on the server.

That's how code looks like, works perfectly fine for me:

const WebSocket = require('ws');

const webSocket = new WebSocket(url, {
  perMessageDeflate: false,
  headers: {
    Authorization: `Basic ${encodedToken}`,
  },
});

webSocket.on('open', function open() {
  console.log('Connection has been established.');
});

I hope it will save you a lot of time.

发布评论

评论列表(0)

  1. 暂无评论