我有一个NodeJS服务器使用socket.io并侦听端口8000.代码是这样的:
var io = require("socket.io"); var socket = io.listen(8000); ... socket.sockets.on("connection", function(client) { util.log("Client connects: " + client.id); });我写了一个可以连接到这个服务器的Web客户端,它运行得很好。 所以服务器代码不是问题。 问题在于我接下来要描述的iOS客户端。
iOS客户端使用SocketRocket并安装它,我使用Cocoapods并有一个Podfile,其中包含以下内容:
platform :ios pod 'SocketRocket', '0.2.0'在MyViewController.h中
#import <SocketRocket/SRWebSocket.h> @interface MyViewController : UIViewController<SRWebSocketDelegate> ... @property (strong, nonatomic) SRWebSocket *socket; @end在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); }iOS客户端运行时,会显示“连接”日志。 但委托didCloseWithCode()也被调用。 日志显示
Error didCloseWithCode 0: Stream end encountered我花了很多时间试图找出答案,但现在是时候从专家那里得到一些帮助了。 为什么我会遇到“Stream end”问题。 NodeJS服务器运行正常,因为我可以使用Web客户端连接到它。 我在iOS客户端做错了什么,我该如何纠正?
更新:在服务器上添加JavaScript代码以显示util.log()调用。 当Web客户端连接时,控制台显示“客户端连接”...“消息。当iOS客户端尝试连接时,控制台上没有任何内容。
I have a NodeJS server that uses socket.io and listen to port 8000. The code is 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 have written a web client that can connects to this server and it works just fine. So the server code is not the problem. The problem is with the iOS client that I am about to describe next.
The iOS client uses SocketRocket and to install it, I use Cocoapods and have a Podfile 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; @endIn 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 runs, the log "Connect" shows up. But the delegate didCloseWithCode() also gets invoked. And the log shows
Error didCloseWithCode 0: Stream end encounteredI have spent lots of time trying to figure out, but it's time to get some help from experts. Why am I getting the "Stream end" problem. The NodeJS server is working just fine, since I can connect to it with a web client. What am I doing wrong with the iOS client and how can I correct?
Update: Add JavaScript code on the server to show the util.log() call. When the web client connects, console shows "Client connects"..." message. When the iOS client attempts to connect, nothing on the console.
最满意答案
事实证明,socket.io有自己的身份验证协议,并且与开箱即用的SocketRocket不兼容。 看到这个SO答案:
如何在socket.io中使用SocketRocket?
从那里的答案,你可以看看socket.io-objc
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 SocketRocket with socket.io?
From the answers there, you could take a look at socket.io-objc
更多推荐
发布评论