2017-07-18 38 views
0

我想將socket.io集成到我的keystone應用程序中。我實例化對象在我keystone.app的底部,像這樣:Socket.io和Keystone - 參考socket.io對象

var sock = require('socket.io'); 

keystone.start({ 
     onStart: function() { 

      var hserver = keystone.httpServer;     
      var io = keystone.set('io', sock.listen(hserver)).get('io'); 

      // Socket function 
      io.on('connection', function (socket) { 
       console.log('Socket connected.') 

       socket.on('hon', function (data) { 
        socket.emit('hon_res', {data}) 
       }); 
      }); 
     } 
    }); 

這一切工作正常,我能夠發出從我收到的插座的消息。當我試圖在我的模型之一用於將數據插入到mongo數據庫後發出消息時,問題就出現了。

在我的架構/模型文件中。我已經添加了這部分。它被正確調用,但套接字似乎無法連接到套接字會話。

Hon.schema.post('save', function(data){ 
    var io = keystone.get('io'); 
    io.attach(keystone.httpServer) 

    io.sockets.emit('hon_upd'); 
}); 

我收到的客戶端控制檯此錯誤,後保存功能,幾秒鐘後稱爲:

VM7414 extensions::messaging:79 Uncaught Error: Attempting to use a disconnected port object 
    at PortImpl.postMessage (extensions::messaging:79:13) 
    at Port.publicClassPrototype.(anonymous function) [as postMessage] (extensions::utils:149:26) 
    at destroyPurpleBox (file:///Users/thobyy/Library/Application%20Support/Google/Chrome/Default/Extensions/mlomiejdfkolichcflejclcbmpeaniij/7.2.2_0/dist/purplebox.js:43:9) 
    at file:///Users/thobyy/Library/Application%20Support/Google/Chrome/Default/Extensions/mlomiejdfkolichcflejclcbmpeaniij/7.2.2_0/dist/purplebox.js:51:5 
+0

你找到一個方法來集成插座,帶梯形JS io的..? – usama

回答

0

你爲什麼要安裝IO一次凌海HttpServer的。

io.attach(keystone.httpServer) 

嘗試刪除上面的行