Connect to telnet via webpage

落花浮王杯 提交于 2019-12-25 08:16:17

问题


I'm new to telnet and sockets and right now I'm all confused over its concepts. So I can use some help here.

So far I've created a simple telnet server using Node.js's package net. Here's a little snippet of that,

function newSocket(socket) {
  sockets.push(socket);
  console.log('A new telnet connection!\n');
  socket.on('data', function(data) {
  });
  socket.on('end', function() {
    console.log('A Telnet socket disconnected!\n');
  });
}

var telnetServer = net.createServer(newSocket);
telnetServer.listen(8080); 

It works perfectly on using command

telnet localhost 8080

and also I know I can connect to it using net package of NodeJS, like this

var client = net.connect({port: port}, function() {
    console.log('client connected');
    client.write('data!\r\n');
});

So, what I want to do is to connect to this telnet server from my client side application (AngularJS), if it's even possible.

I know how to connect to web sockets from AngularJS app using $websocket service, like this,

      $websocket.$new({
        url: websocketUrl,
        protocols: [],
        subprotocols: ['base46'],
        reconnect: true,
        reconnectInterval: 500
      });

So, my questions are:

  • Can I connect to telnet server from client-side? I mean is it even possible to connect to telnet from web browser like we do with web-sockets?
  • If yes, can you show me some kind of snippet for that?
  • Are web-sockets are better option than Telnet?

I know these questions sound silly, but like I said I have just started digging around about these things. Thanks for your time.


回答1:


After going through various docs online, I got my answer. And as there is no answer yet (as of writing this), I am posting, what I found, one myself.

First of all, Answers of my Questions

Can I connect to telnet server from client-side? I mean is it even possible to connect to telnet from web browser like we do with web-sockets?

No, telnet is a protocol for the server, not some way to communicate between client and server. This was my biggest confusion which pushed me to ask this question here.

If yes, can you show me some kind of snippet for that?

Well, we can't, so no snippet.

Are web-sockets are better option than Telnet?

Both are different, so there is no comparison.

Solution

So what I did is, I used

  • a server to host telnet (telnet server)
  • another server for web-sockets (telnet client)
  • client-side (webview)

So by this approach, I established a connection between telnet and websockets (which I needed in the first place), both are hosted on different server, and the server which was listening to web-socket, acted as telnet client.



来源:https://stackoverflow.com/questions/41545128/connect-to-telnet-via-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!