Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementation of SocketIO in ios
    primarykey
    data
    text
    <p>I want to implement <a href="http://socket.io/" rel="nofollow">SocketIO</a> in my project. </p> <p>The serverside and client side codes for my project is given below. </p> <p>At the client side, there is a test button which sends a post request(<code>[self.afn post:@"/test" withParams:@{} completion:^(id obj{})]</code>) to the server side. self.afn is my instance object of my wrapper class for <a href="https://github.com/AFNetworking/AFNetworking" rel="nofollow">AFNetworking library</a>.</p> <p>The server side catches this post request with the function <code>app.post('test', function(){...}</code> In this post request handler, I emit a data(<code>{hello: "world"}</code>) at the <code>news</code>channel and I expect to catch this data on the client side in the <code>didReceiveMessage</code> handler of the <a href="https://github.com/pkyeck/socket.IO-objc" rel="nofollow">SocketIO library for Objective C</a>.</p> <p>I checked that, the button successfully sends the post request, and server side successfully handles this post request and emits the data(<code>hello:"world"</code>) on the <code>news</code> channel. However, client side does not catch this data on the <code>didReceiveMessage</code> handler.</p> <p>Where is the problem? Do you have any idea?</p> <p>The details of the codes for server and client side are given below:</p> <p>The ServerSide:</p> <pre><code>//server.js var express = require('express'); var app = module.exports = express(); /* configure app ... */ var io = require('socket.io').listen(8090); var mySocket; io.sockets.on('connection', function (socket) { mySocket = socket; }); app.post("/test", function(req, res){ if(mySocket){ mySocket.emit('news', { hello: "world"}); } }); </code></pre> <p>The ClientSide:</p> <pre><code>//Client side in ObjectiveC @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.txtFrom.delegate = self; self.txtTo.delegate = self; //initialize socket self.socket = [[SocketIO alloc] initWithDelegate:self]; [self.socket connectToHost: @"172.20.10.5" onPort:8090]; } // IBAction for the test button to send a post request to the server to activate - (IBAction)actTest:(id)sender { //self.afn is my wrapper class for AFNetworking [self.afn post:@"/test" withParams:@{@"foo": @"bar"} completion:^(id responseObj){ NSLog(@"Response object: %@", responseObj); }]; } - (void) socketIO:(SocketIO *)socket didReceiveMessage:(SocketIOPacket *)packet { NSLog(@"didReceiveMessage() &gt;&gt;&gt; data: %@", packet.data); } @end </code></pre> <p><strong>EDIT:</strong></p> <p>I followed the suggestion of Mathieu 'OtaK' Amiot and changed <code>[self.socket connectToHost: @"172.20.10.5" onPort:8090 withParams:nil withNamespace:@"news"];</code>to <code>[self.socket connectToHost: @"172.20.10.5" onPort:8090];</code></p> <p>Now, I can see the message at the console, however, when the message is received at the client side, <code>didReceiveMessage</code>handler does not invoked. </p> <p>When I'm sending the message from sender, the name of the event is "news", but in the client side I did not mention an event name. Can this be the reason? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload