javascript - Node.js 服务器的哪个 Websocket 库最适合 iOS 客户端?

标签 javascript ios node.js websocket server

在 iOS 客户端上,我使用 Square 的 SocketRocket:https://github.com/square/SocketRocket

我到处都看到了基于从浏览器访问或在数据库中查询的 Web 应用程序的 Websocket 库的比较,但对于 iOS 智能手机应用程序的客户端还没有发现。

客户端将根据应用程序的请求连接到远程服务器(即连接不是“始终在线”或通过移动浏览器或代理或 GameCenter 完成),并且一旦连接,就会与其他客户端配对在两人“游戏”的情况下。在比赛结束之前,连接需要持续存在,服务器将负责为每个用户的回合计时,并从/向每个用户接收和发出命令,有点像回合制游戏,除了每个回合都有一个服务器管理的时限。一场比赛结束后(通常为 15-20 分钟),如果用户不想与另一个随机对手进行另一场比赛,则连接将关闭并且用户注销;想要继续的用户将由托管服务器(运行 Node.js 和 Websocket 库)与另一个用户匹配。

我考虑过的一些选项包括 Socket.IO 1.0:http://socket.io/ Sockjs:https://github.com/sockjs ws:https://github.com/einaros/ws nodejs-websocket:https://www.npmjs.com/package/nodejs-websocket

但收到 https://medium.com/@denizozger/finding-the-right-node-js-websocket-implementation-b63bfca0539 的消息Socket.IO 对于大量用户流量来说并不是最佳选择(我预计在任何时候都会有超过 300 名用户请求匹配),并且 Sockjs 没有一些命令查询功能,但并没有找到一个决定性的在智能手机或 iOS 设备(而非浏览器)的上下文中回答,无论哪种方式,在任何情况下。

问题是,对于运行 SocketRocket 的 iOS 客户端,哪种 Node.js 服务器 Websocket 库可能发挥得最好或接口(interface)的稳定性/可扩展性/复杂性问题最少? SocketRocket wiki 本身没有帮助,因为它使用基于 Python/Go 的服务器端测试。

编辑:可能有用的资源: http://www.teehanlax.com/blog/how-to-socket-io-swift/

唯一缺少的是对其他潜在 websocket API 的比较或讨论,而不仅仅是 Socket.IO。但这是一个开始,因为它似乎可以使用最新的 iOS、SocketRocket 和 Socket.IO 版本。

最佳答案

我喜欢Sockjs,因为它很简单。这是 SocketRocket 的实现 --> Sockjs,可作为概念证明

需要: -SocketRocket(将 libicucore.dylib、Security.framework 和 CFNetwork.framework 添加到您的项目中) -Node.js -Sockjs服务器

服务器:

var http = require('http'),
    sockjs = require('sockjs'),
    sockserver = sockjs.createServer(),
    connections = [];

sockserver.on('connection', function(conn) {
  console.log('Connected');
  connections.push(conn);
  conn.on('data', function(message) {
    console.log('Message: ' + message);
    // send the message to all clients
    for (var i=0; i < connections.length; ++i) {
      connections[i].write(message);
    }
    //
  });
  conn.on('close', function() {
    connections.splice(connections.indexOf(conn), 1); // remove the connection
    console.log('Disconnected');
  });
});

var server = http.createServer();
sockserver.installHandlers(server, {prefix:'/sockserver'});
server.listen(3000, '0.0.0.0'); // http://localhost:3000/sockserver/websocket

客户端(ViewController.m):

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    SRWebSocket *myWebSocket;

    __weak IBOutlet UILabel *connectionStatus;
    __weak IBOutlet UITextView *myTextView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    connectionStatus.textColor = [UIColor redColor];
    myWebSocket = [[SRWebSocket alloc] initWithURL:[[NSURL alloc] initWithString:@"http://localhost:3000/sockserver/websocket"]];
    myWebSocket.delegate = self;
    [myWebSocket open];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{

    myTextView.text = message;
    NSLog(@"message: %@",message);
}

- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean{

    connectionStatus.text = @"Disconnected";
    connectionStatus.textColor = [UIColor redColor];
}

- (void)webSocketDidOpen:(SRWebSocket *)webSocket{

    connectionStatus.text = @"Connected";
    connectionStatus.textColor = [UIColor greenColor];
}


- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error{

}

@end

源代码:http://nunoferro.pt/?p=22

关于javascript - Node.js 服务器的哪个 Websocket 库最适合 iOS 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832823/

相关文章:

javascript - JavaScript 中的 <% 和 %> 是什么意思?

javascript - $window.addEventListener 未在 Angular 中触发

ios - UITableView 和 UITableViewHeader 在旋转和调整大小后发生冲突

android - soomla : cocos2dx : Profile -> "ProviderNotFoundException" (cocos2d3. x)

javascript - TypeError : 'caller' , 'callee' 和 'arguments' 属性可能无法在严格模式函数或调用的参数对象上访问

javascript - 如何比较字符串并检查阈值是否相等?

ios - 如何从字符串数组中删除图像?

regex - 使用 MongoDB 和 Mongoose 在多个字段中搜索子字符串

node.js - npm安装oracle失败

json - 在 NodeJs 中读取 JSON 文件