4
我正在使用套接字io的sailsjs。風帆版本是0.10.5。我有以下套接字客戶端來進行測試:在sails js socket.io中使用io.socket.get
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// 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';
// ...
//Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/join', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
//io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
io.socket.on('myroom', function(response){
console.log(response);
});
在我的配置/ routes.js,我有以下幾點:
'get /join':'LayoutController.join'
在我的API /控制器/ LayoutController,我有以下幾點:
module.exports={
join: function(req, res) {
console.log('Inside Join');
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
};
我遇到的問題是控制器內的連接函數永遠不會通過套接字調用io.socket.get('/ join',...)被觸發。控制器中的console.log不會打印,也不會迴應客戶端。如果我通過http執行相同的測試,它會觸發連接功能。我正在與套接字第一次。任何人都可以請建議我在這裏做錯了嗎?
你把你的套接字客戶端代碼放在哪裏?你是從單獨的Sails或Node服務器運行它嗎?它看起來不像代碼意味着在瀏覽器中運行,除非你使用'browserify' ... – sgress454
感謝您的回覆sgress。我正在使用節點分別運行socketclient代碼。最終會有一個客戶。但是,這是爲了我的測試。這似乎是我的本地機器的問題。我在另一臺服務器上獲得了io.socket.get(...)的響應。另一個問題,不相關,用於sails的socket.io適用於IIS 7.5嗎? – Ajith
Socket.io不是由Sails相同的開發人員維護的,但谷歌搜索「socket.io IIS」應該給你一些見解。如果你能弄清楚你的本地機器出了什麼問題,我會建議改變你的標題以反映你的系統(例如「Socket請求不適用於Windows Server 2012」),然後發佈一個答案,以便其他人也可以解決相同的問題可能在這裏找到幫助! – sgress454