Socket.io Javascript nodejs, TCP 套接字

Socket.io Javascript nodejs, TCP socket

I am trying to send a TCP Request to a server via IP:PORT...

这段代码对我有用:

var net = require('net'); var client = new net.Socket();
const port = 1337; const HOST = "192.168.1.101";
function connect() {
  client.connect({ port: PORT, host: HOST });
  client.on('data', (data) => {
    console.log(data);
    client.destroy();
    });
  
    client.on('close', function() {
    console.log('Connection closed');
    });
  }

此代码挂起,无响应: (另请注意:我在 io.connect 中的 IP 之前输入 'http://' 以响应未 return 未定义的主机名(见下文)):

console.log(socket); //returns=> undefined://192.168.1.101:1337
//uri: 'undefined//192.168.1.101:730',


const io = require('socket.io-client');
const socket = io.connect('192.168.1.101:1337');//=undefined hostname; works, but hangs
//const socket = io.connect('http://192.168.1.101:1337'); //works, but hangs as well...

function connect() {
  console.log('connecting...');
  **socket.on('connect', () => {**
    console.log('Successfully connected!');
      // add handlers for socket events
  }); //I never receive response from socket.on('connect', ()=> {...

无论我尝试发送套接字,我都没有收到响应... 它只是挂起,等待响应...

如有任何帮助,我们将不胜感激!

console.log(socket); //this command returns the following response from command down below:

套接字 { 连接:假, 断开连接:真, 接收缓冲区:[], 发送缓冲区:[], 编号:0, 确认:{}, 标志:{}, io:经理{ nsps: { '/': [Circular *1] }, 潜艇:[ [功能:subDestroy], [功能:subDestroy], [功能:subDestroy] ], 选择:{ 路径:'/socket.io', 主机名:'192.168.1.101', 安全:假的, 端口:'1337' }, setTimeoutFn: [功能:绑定setTimeout], clearTimeoutFn: [功能:绑定clearTimeout], _重新连接:是的, _reconnectionAttempts:无限, _reconnectionDelay:1000, _reconnectionDelayMax:5000, _randomizationFactor:0.5, 退避:退避{ 女士:1000, 最大值:5000, 因素:2, 抖动:0.5, 尝试次数:0 }, _超时:20000, _readyState: 'opening', uri: 'http://192.168.1.101:1337', 编码器:编码器{}, 解码器:解码器{}, _autoConnect:真, 引擎:套接字{ setTimeoutFn: [功能:绑定setTimeout], clearTimeoutFn: [功能:绑定clearTimeout], 安全:假的, 主机名:'192.168.1.101', 端口:'1337', 运输:[数组], 就绪状态:'opening', 写缓冲区:[], 上一个缓冲长度:0, 选择:[对象], 编号:空, 升级:空, pingInterval:空, ping超时:空, pingTimeoutTimer:空, 运输:[XHR], _回调:[对象] }, skipReconnect:假, _回调:{ '$open': [数组], '$数据包':[数组], '$error': [数组], '$close': [数组] } }, nsp: '/', 潜艇:[ [功能:subDestroy], [功能:subDestroy], [功能:subDestroy], [功能:subDestroy] ] } */

您忘记在 URL 中声明协议,请尝试 http://192.168.1.101:1337ws://192.168.1.101:1337(如果您的服务器不使用 SSL)

并确保服务器端也实现 socket.io 服务器而不仅仅是 TCP 协议。