【问题标题】:geddy with socket.io errorsocket.io 错误的 geddy
【发布时间】:2013-11-14 11:45:24
【问题描述】:

我正在使用 geddy 框架(基本聊天示例)做一个实时应用程序。但是当客户端尝试建立连接时出现错误。

这是服务器端代码(在 init.js 文件中):

var io = require('socket.io').listen(geddy.server);
io.sockets.on('connection', function (socket) {
  console.log("Good!");
  socket.emit('new', { message: 'world' });
  socket.on('newMessage', function (data) {
    console.log(data);
  });
});

和客户端代码:

$(document).ready(function(){
    startSockets();
});

function startSockets(){
  var socket = io.connect('http://localhost:4004');
  socket.on('new', function (data) {
    alert(data);
    //socket.emit('newMessage', { my: 'data' });
  });
}

当我尝试连接到 localhost:4004/ 时,我收到下一个警告:

   debug - setting request GET /socket.io/1/websocket/G_GapksVv1J4iBZIUVe3
   debug - set heartbeat interval for client G_GapksVv1J4iBZIUVe3
   debug - websocket writing 7:::1+0
   warn  - client not handshaken client should reconnect
   info  - transport end (error)
   debug - set close timeout for client G_GapksVv1J4iBZIUVe3
   debug - cleared close timeout for client G_GapksVv1J4iBZIUVe3
   debug - cleared heartbeat interval for client G_GapksVv1J4iBZIUVe3
   debug - discarding transport

除了 Chrome 控制台给出了这个错误:

WebSocket is closed before the connection is established. 

我不知道是什么导致了这些。有什么想法吗?

【问题讨论】:

    标签: node.js socket.io geddy


    【解决方案1】:

    问题是 Socket.io 启动后需要连接服务器,而服务器要等到 init.js 中的代码运行后才会启动。现有内置 RT 代码(从 Geddy v0.11 开始)中的当前(hacky)解决方案是将此类代码放在 config 目录中的 after_start.js 文件中,Geddy 在服务器启动后运行该文件。在这种情况下,这也应该是一种解决方法,您自己将 Socket.io 连接到服务器。

    这显然不理想,v0.12 的一个主要目标是修复 RT 集成,使其更加实用和有用。如果您对您认为的外观有意见,请务必在 IRC(Freenode.net 上的#geddy)或邮件列表 (https://groups.google.com/group/geddyjs) 上联系我们。

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 1970-01-01
      • 2017-07-22
      • 2018-06-20
      • 2012-05-25
      • 2017-09-01
      • 1970-01-01
      • 2014-09-22
      • 2012-11-15
      相关资源
      最近更新 更多