gpt4 book ai didi

node.js - socket.on 函数中未触发 Socket.get 回调

转载 作者:可可西里 更新时间:2023-11-01 02:54:24 26 4
gpt4 key购买 nike

我已经在这个问题上停留了一段时间,答案可能真的很基本,但我不明白问题是什么。 AFAIU 它执行函数但不触发回调,我不知道为什么。

我的脚本旨在让 tcp 服务器拥有连接 tcp 套接字的设备(树莓派)和客户端连接到 sailsjs 应用程序上的 websocket。

我设法让这两个东西都在下面的代码上运行,问题是它们只能单独工作,同时但分开工作,当我尝试从套接字外部获取时一切正常,但是当我在套接字内部执行时,io.socket 对象只是把 get 请求堆在一个 requestQueue 中。

{ useCORSRouteToGetCookie: true,
url: 'http://localhost:1337',
multiplex: undefined,
transports: [ 'polling', 'websocket' ],
eventQueue: { 'sails:parseError': [ [Function] ] },
query:'__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript',
_raw:
{ socket:
{ options: [Object],
connected: true,
open: true,
connecting: false,
reconnecting: false,
namespaces: [Object],
buffer: [],
doBuffer: false,
sessionid: '0xAlU_CarIOPQAGUGKQW',
closeTimeout: 60000,
heartbeatTimeout: 60000,
origTransports: [Object],
transports: [Object],
heartbeatTimeoutTimer: [Object],
transport: [Object],
connectTimeoutTimer: [Object],
'$events': {} },
name: '',
flags: {},
json: { namespace: [Circular], name: 'json' },
ackPackets: 0,
acks: {},
'$events':
{ 'sails:parseError': [Function],
connect: [Object],
disconnect: [Function],
reconnecting: [Function],
reconnect: [Function],
error: [Function: failedToConnect],
undefined: undefined } },
requestQueue:
[ { method: 'get', headers: {}, data: {}, url: '/', cb: [Function] },
{ method: 'get', headers: {}, data: {}, url: '/', cb: [Function] } ] }

代码如下:

//library to connect to sailsjs websockets
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');

//library to do the tcp server
var net = require('net');


// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';



var server = net.createServer(function(tcpSocket) { //'connection' listener


//socket was sucessfully connected
console.log('client connected');

//notify on deconnection
tcpSocket.on('end', function() {
console.log('client disconnected');
});

// Handle incoming messages from clients.
tcpSocket.on('data', function (data) {


console.log(data.toString('utf8', 0, data.length));

//if data is PING respond PONG
if(data.toString('utf8', 0, 4)=='PING'){
console.log('I was pinged');
tcpSocket.write('PONG\r\n');
}

console.log(io.socket);//debugging purpose
//trigger a socket call on the sails app
io.socket.get('/', function (body, JWR) {
//display the result
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);

});
});

});


server.listen(8124, function() { //'listening' listener
console.log('server bound');
});

最佳答案

看起来您的套接字没有自动连接。尝试手动连接:

// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';

var socket = io.sails.connect();

socket.on('connect', function() {

... connect TCP server and continue ...

});

关于node.js - socket.on 函数中未触发 Socket.get 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com