简体   繁体   English

Node.js socket.io不响应iOS socket.io发出请求

[英]Node.js socket.io not responding to iOS socket.io emit requests

I can't seem to figure this one out. 我似乎无法弄清楚这一点。 I'm testing the usage of socket.io to connect an iOS client app with a Node.js server. 我正在测试使用socket.io将iOS客户端应用程序与Node.js服务器连接。 My server code looks like this: 我的服务器代码如下:

var io = SocketIO(8099, { log: true });

io.on('connection', function (socket) {

    socket.on('connect', function (data) {
            console.log('Connected, data: ' + data);
        });

    socket.on('getProjects', function(from, msg) {
        database.allProjects(function getAllProjectsCallback(err, rows) {
            console.log('Got the project list');
            socket.emit('projectList', rows);
        });
    });

});

My iOS code starts with: 我的iOS代码开头为:

_socket = [[SocketIOClient alloc] initWithSocketURL:@"http://localhost:8099" opts:nil];

    [_socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"socket connected");
    }];

    [_socket on:@"projectList" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"Project list received");
        [ack with:@[]];
    }];

    [_socket connect];

And in another method I have this code which should send the get projects request: 在另一种方法中,我有这段代码应发送get项目请求:

[_socket emit:@"getProjects" withItems:@[]];

I can see the iOS "socket connected" message in the log. 我可以在日志中看到iOS“套接字已连接”消息。 But not the Node.js "Connected, ..." message. 但不是Node.js的“已连接...”消息。 When I trigger the [_socket emit:@"getProjects" ...] in iOS, nothing happens on the server. 当我在iOS中触发[_socket emit:@"getProjects" ...]时,服务器上没有任何反应。 Neither the 'connect' or 'getProjects' events are called. 'connect'或'getProjects'事件均未调用。

I've trolled the internet and as far as I can tell, I've done everything as per all the blogs and examples I can find. 我已经浏览了互联网,据我所知,我已经完成了所有可以找到的博客和示例的所有工作。

What am I missing in this setup? 我在此设置中缺少什么? Or have I fundamentally miss-understood something? 还是我从根本上错过了一些事情?

I have no knowledge about iOS code. 我对iOS代码一无所知。 But I saw something wrong in your server code. 但是我发现您的服务器代码有问题。

 socket.on('connect', function (data) {
        console.log('Connected, data: ' + data);
 });

Above code mean when someone send data to socket name "connect" its will console.log that data. 上面的代码意味着当有人向套接字名称“ connect”发送数据时,它将console.log该数据。

I think this is what your code should look like. 我认为这是您的代码应具有的外观。

var io = SocketIO(8099, { log: true });

io.on('connection', function(socket) {
    console.log('New Connection');
    socket.on('getProjects', function(data) {
        database.allProjects(function getAllProjectsCallback(err,  rows) {
            console.log('Got the project list');
            socket.emit('projectList', rows);
        });
    });
});

Just realised this was all my bad. 刚意识到这是我的全部不幸。 I had the timing wrong because I was refactoring from code that worked with a different comms model. 我选择了错误的时间,因为我是从使用其他通信模型的代码中重构的。 My code was managing to execute the emit after the socket had been created but before it was ready to send data. 我的代码设法在创建套接字之后但准备好发送数据之前执行发射。

Everything working now. 现在一切正常。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM