Testing node websockets with telnet
Asked Answered
R

3

7

I am writing a socket-based server in Node js using the ws library, and I would like to test that my code works. I have seen telnet used elsewhere to test simple chat servers, but when I start my server and execute telnet 127.0.0.1 5000, although the output says "connected to localhost", my server doesn't log anything associated with a new connection. Am I testing my server wrong or is my server simply not working? My server code is below:

    var WebSocketServer = require('ws').Server
      , http = require('http')
      , express = require('express')
      , app = express()
      , port = process.env.PORT || 5000;

    var server = http.createServer(app);
    server.listen(port);

    var wss = new WebSocketServer({server: server});
    console.log('websocket server created');
    wss.on('connection', function(ws) {
    var id = setInterval(function() {
        ws.send(JSON.stringify(new Date()), function() {  });
    }, 1000);

    console.log('websocket connection open');

    ws.on('close', function() {
        console.log('websocket connection close');
        clearInterval(id);
    });
   });
Rubyeruch answered 11/1, 2014 at 5:18 Comment(1)
It looks like you've figured this out, but WebSockets aren't just plain old TCP sockets. It's a whole application layer protocol. There's a lot to it, so you won't be able to test with a Telnet client.Exotoxin
B
6

You connected to the HTTP server, but did not establish a WebSocket connection. That's why your script doesn't print anything.

I'm not sure you can test websockets manually, look at the handshake.

But there are a few telnet-like programs that work with websocket. Maybe wscat from the ws module you're using will help with that.

Boatright answered 11/1, 2014 at 5:25 Comment(1)
is there a way to connect to the socket from an iOS application? passing the ip address and the port, with no luck connecting ( here's my code: CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"122.38.1.3", 91 , &readStream, &writeStream);)Rubyeruch
D
1

I found https://www.websocket.org/echo.html To be useful. Just create a websocket.index file on your hard-drive if you are testing a localhost server. Code is on the bottom of the page.

Deteriorate answered 2/2, 2020 at 10:10 Comment(0)
M
0

UPD: as per @jouell's note, it looks like this tool is now out of date


This little tool did the job for me: https://github.com/lafikl/telsocket

brew tap pascaliske/telsocket
brew update
brew install telsocket
telsocket -url ws://127.0.0.1:50000

(for people who don't use npm)

Molasses answered 12/6, 2017 at 3:0 Comment(2)
Warning: if you follow the authors like on github (telsocket dot org) it brings the user to an adult site. That makes me think this tool is malware....I wondered what was going on - thought was like the hosted micropython webreplVouchsafe
@Vouchsafe that's true. The website says "buy this domain" and the last update to the repo has been 3 years ago. Looks like this tool is not maintained anymore.Molasses

© 2022 - 2024 — McMap. All rights reserved.