gpt4 book ai didi

javascript - 在套接字 io 上使用 Node 请求库时出错

转载 作者:行者123 更新时间:2023-11-28 05:13:14 25 4
gpt4 key购买 nike

我在 Laravel 项目上使用套接字 io,但在 socket.js 文件中的断开连接事件上的请求出现问题。

我已经调试了我的代码,错误出在请求库上。

Socket.js

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(5000);

io.on('connection', function (socket) {
console.log("new client connected " + socket.id);

socket.on('disconnect', function() {
var socket_id = socket.id;
console.log('client disconnected :C '+socket_id);

var request = require('request');
var url_request = 'http://example.com/play-session/close?socket_id=' + socket_id;
request(url_request, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('session closed '+socket_id);
}
});
});
});

错误:

SyntaxError: Error parsing /var/www/example.com/public_html/node_modules/aws4/package.json: Unexpected token <
at Object.parse (native)
at readPackage (module.js:113:52)
at tryPackage (module.js:123:13)
at Function.Module._findPath (module.js:190:18)
at Function.Module._resolveFilename (module.js:336:25)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/example.com/public_html/node_modules/request/request.js:11:12)
at Module._compile (module.js:456:26)

我不知道发生了什么。我感谢所有的帮助,谢谢。

已更新,问题已修复:

我用这个解决了这个问题: https://www.npmjs.com/package/request#unix-domain-sockets ,使用这个例子:

Socket.js

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
var request = require('request');

server.listen(5000);

io.on('connection', function (socket) {
console.log("new client connected " + socket.id);

socket.on('disconnect', function() {
var socket_id = socket.id;
console.log('client disconnected :C '+socket_id);

var url_request = 'http://example.com/play-session/close?socket_id='+socket_id;
request.get(url_request); // this line fix it
});
});

感谢大家的帮助。

最佳答案

您可以通过以下方式解决此问题:https://www.npmjs.com/package/request#unix-domain-sockets ,使用这样的例子:

request.get(url_request);

问候

关于javascript - 在套接字 io 上使用 Node 请求库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202751/

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