2016-12-17 79 views
1

我在我的laravel項目上使用套接字io,並且在socket.js文件中的斷開連接事件請求上出現問題。錯誤在套接字上使用節點請求庫io

我調試了我的代碼,錯誤與請求庫有關。

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

感謝所有您的幫助。

+1

看起來像'aws4'導致問題。嘗試重新安裝它 – Shaharyar

+0

好的,我會嘗試再次在我的服務器中重新安裝請求庫。 –

+0

不是'請求',但['aws4'](https://www.npmjs.com/package/aws4) – Shaharyar

回答