0

I'm trying to write a simple websocket client to connect to a device. However the device expects a username and password and I'm having quite a bit of trouble finding how to pass that with the connection.

This is one of the ways I've tried it.

var io = require('socket.io-client'),
    socket = io.connect('192.168.1.25/eventstream', 
    {username: 'Admin', password: 'Admin'});

I'm sorry if this is something simple, node/socket.io are new to me so searching with the right terms is difficult. Any links are appreciated.

sk1tt1sh
  • 198
  • 1
  • 2
  • 11
  • do you have documentation for the device? I suspect that username and password are supposed to be passed after a successful connection. – trex005 Jan 23 '15 at 21:10
  • Yes the "Authorization: Basic QWRtaW46QWRtaW4=" header is all it shows though. Full Request URI: ws://192.168.60.80/stream=true HTTP - WebSocket /stream=true HTTP/1.1 Host: 192.168.60.80 Sec-WebSocket-Version: 13 Sec-WebSocket-Key: sL7YXg61Aqzr7HXA3M9Gow== Authorization: Basic QWRtaW46QWRtaW4= Connection: keep-alive, Upgrade Upgrade: websocket – sk1tt1sh Jan 23 '15 at 21:19
  • this is just a stab in the dark, but since it's basic authentication you can try `io.connect('http://Admin:Admin@192.168.1.25/eventstream')` – trex005 Jan 23 '15 at 21:24
  • Nope :( that didn't work. When I do io.on('connect'.... it spits out "connected: false, disconnected: true" – sk1tt1sh Jan 23 '15 at 21:29
  • 1
    have you tried `socket = io.connect('192.168.1.25/eventstream', {Sec-WebSocket-Version: 13, Sec-WebSocket-Key: 'sL7YXg61Aqzr7HXA3M9Gow==', Authorization: 'Basic QWRtaW46QWRtaW4=', Connection: 'keep-alive, Upgrade', Upgrade: 'websocket'}` ? – trex005 Jan 23 '15 at 21:56
  • What software is the device running? [socket.io clients can't connect to a WebSocket server](http://stackoverflow.com/questions/22232023/can-i-use-socket-io-client-to-connect-to-a-standard-websocket/22235945#22235945) - they're very different protocols. – Aaron Dufour Jan 23 '15 at 22:23
  • Wow thanks Aaron. I didn't even see that. Does the same apply for other JS based "WebSocket" packages? This is actual websocket server I'm trying to connect to. – sk1tt1sh Jan 26 '15 at 13:54
  • @sk1tt1sh You should probably just use the built-in `WebSocket` constructor in the browser. It's fairly usable - no real need to get a library. As for other WebSocket packages, it varies. Is there a specific one you're looking at? – Aaron Dufour Jan 26 '15 at 15:10
  • @AaronDufour I'm actually using node through an intel edison. Not a browser. I'm trying to connect to a device that has a stream output of events over websockets. I really just need to authenticate and get the event messages. I'm trying https://github.com/theturtle32/WebSocket-Node right now. I've tried the stuff listed above as well as making an options object with the auth header to no avail. I'm kind of surprised this is so difficult. – sk1tt1sh Jan 26 '15 at 15:48
  • Alright weird... I think node did some behind the scenes stuff for me. This seems to have either created a the websocket. I need to wireshark it but it's sending me the expected data: `var http = require('http'); var options = { host: '192.168.150.202', port: 80, path: 'stream?Events=true', headers: { 'Authorization': 'Basic QWRtaW46QWRtaW4=' } }; request = http.get(options, function(res){ res.on('data',function(data){ console.log(data.toString()); }); }); ` – sk1tt1sh Jan 26 '15 at 15:51
  • If you're using the `http` lib, it's definitely not a WebSocket. Are you sure that's doing what you think it is? – Aaron Dufour Jan 26 '15 at 16:05
  • Not sure at all... It's strange that it's streaming events so quickly. I was reading the http lib doc for node and it says when the upgrade event occurs and the client is not listening for it `client.on('upgrade'....` it will close the connection so I'm pretty confused. Basically all I need is a websocket client to get the event stream and give me the xml. I'm hesitant to keep using that http client because I don't want it to just drop/close randomly. – sk1tt1sh Jan 26 '15 at 16:45

1 Answers1

1

Woot! I feel a bit sheepish but...using https://github.com/theturtle32/WebSocket-Node I was able to get a connection.

var WebSocket = require('websocket').client;
var client = new WebSocket();
...
client.connect('ws://192.168.1.2', null, null, 
{'Authorization':'Basic QWRtaW46QWRtaW4='}, null);

Likely my lack of familiarity with javascript. Thanks for all the suggestions Aaron & trex005!

sk1tt1sh
  • 198
  • 1
  • 2
  • 11