连接到NodeJS socket.io的iOS客户端导致遇到Stream端(iOS Client connecting to NodeJS socket.io results in Stream en

编程入门 行业动态 更新时间:2024-10-28 06:27:23
连接到NodeJS socket.io的iOS客户端导致遇到Stream端(iOS Client connecting to NodeJS socket.io results in Stream end encountered)

我有一个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; @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 runs, the log "Connect" shows up. But the delegate didCloseWithCode() also gets invoked. And the log shows

Error didCloseWithCode 0: Stream end encountered

I 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

更多推荐

本文发布于:2023-07-29 05:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1312605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   客户端   socket   io   encountered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!