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
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.
source share