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:
您忘记在 URL 中声明协议,请尝试 http://192.168.1.101:1337
或 ws://192.168.1.101:1337
(如果您的服务器不使用 SSL)
并确保服务器端也实现 socket.io
服务器而不仅仅是 TCP 协议。
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:
您忘记在 URL 中声明协议,请尝试 http://192.168.1.101:1337
或 ws://192.168.1.101:1337
(如果您的服务器不使用 SSL)
并确保服务器端也实现 socket.io
服务器而不仅仅是 TCP 协议。