gpt4 book ai didi

iOS Client 连接到 NodeJS socket.io 结果遇到 Stream end

转载 作者:可可西里 更新时间:2023-11-01 05:45:11 26 4
gpt4 key购买 nike

我有一个使用 socket.io 并监听端口 8000 的 NodeJS 服务器。代码是这样的:

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 客户端运行时,会显示“Connect”日志。但是委托(delegate) didCloseWithCode() 也会被调用。并且日志显示

Error didCloseWithCode 0: Stream end encountered

我花了很多时间试图弄清楚,但现在是时候从专家那里获得一些帮助了。为什么我会遇到“流结束”问题。 NodeJS 服务器运行良好,因为我可以使用 Web 客户端连接到它。我在 iOS 客户端上做错了什么,我该如何纠正?

更新:在服务器上添加 JavaScript 代码以显示 util.log() 调用。当 Web 客户端连接时,控制台显示“客户端连接”...”消息。当 iOS 客户端尝试连接时,控制台上没有任何内容。

最佳答案

事实证明,socket.io 有自己的身份验证协议(protocol),并且与开箱即用的 SocketRocket 不兼容。看到这个 SO 答案:

How to use SocketRocket with socket.io?

从那里的答案中,您可以查看 socket.io-objc

关于iOS Client 连接到 NodeJS socket.io 结果遇到 Stream end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16918711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com