在套接字io上使用节点请求库时出错

时间:2016-12-17 20:30:34

标签: javascript node.js laravel socket.io request

我在laravel项目中使用socket io,而socket.js文件中的disconnect事件请求有问题。

我调试了我的代码,错误在于请求库。

Socket.js

try-except

错误:

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);
            }
        });
    });
});

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

已更新,修复问题已完成:

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

Socket.js

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)

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

你可以解决这个问题:https://www.npmjs.com/package/request#unix-domain-sockets,就像这样的例子:

request.get(url_request);

此致