IOS client connected to NodeJS socket.io crashes with thread

I have a NodeJS server that uses socket.io and listens on port 8000. The code looks something like this:

var io = require("socket.io"); var socket = io.listen(8000); ... socket.sockets.on("connection", function(client) { util.log("Client connects: " + client.id); }); 

I wrote a web client that can connect to this server and it works fine. Therefore, the server code is not a problem. The problem with the iOS client, which I am going to describe below.

The iOS client uses SocketRocket and I use Cocoapods to install it and have a subfile with the following:

 platform :ios pod 'SocketRocket', '0.2.0' 

In MyViewController.h

 #import <SocketRocket/SRWebSocket.h> @interface MyViewController : UIViewController<SRWebSocketDelegate> ... @property (strong, nonatomic) SRWebSocket *socket; @end 

In MyViewController.m

 @synthesize socket = _socket; ... - (void)viewDidLoad { _socket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://127.0.0.1:8000"]]]; _socket.delegate = self; [_socket open]; NSLog(@"Connect"); } - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { NSLog(@"Error didReceiveMessage"); } - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { NSLog(@"Error connecting websocket"); } - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { NSLog(@"Error didCloseWithCode %d: %@", code, reason); } 

When the iOS client starts, the Connect log appears. But the didCloseWithCode () delegate is also called. And the log displays

 Error didCloseWithCode 0: Stream end encountered 

I spent a lot of time figuring out, but it's time to get help from experts. Why am I getting the "End Stream" problem. The NodeJS server is working fine since I can connect to it using the web client. What am I doing wrong with the iOS client and how can I fix it?

Update: Add JavaScript code to the server to show util.log () call. When the web client connects, the message “Client is connecting” is displayed on the console ... “When the iOS client tries to connect, nothing is displayed on the console.

+4
source share
1 answer

It turns out that socket.io has its own authentication protocol and is not compatible with SocketRocket out of the box. See this SO answer:

How to use socketocket with socket.io?

From the answers there you can take a look at socket.io-objc

+3
source

Source: https://habr.com/ru/post/1484408/


All Articles