0

I'm doing a realtime app with geddy framework (the basic chat example). But I get and error when the client tries to establish the connection.

here's the server-side code (on the init.js file):

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

and the client-side code:

$(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' });
  });
}

When I try to connect to localhost:4004/ I get the next warn:

   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

besides Chrome console gives this error:

WebSocket is closed before the connection is established. 

I don't know what can cause these. Any ideas?

4

1 回答 1

0

问题是 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)上联系我们。

于 2013-11-16T06:10:56.103 回答