簡體   English   中英

Node.js服務器的哪個Websocket庫最適合iOS客戶端?

[英]What Websocket library for a Node.js server works best with iOS clients?

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

我看過的所有地方都發現了基於Websocket庫的比較,這些Websocket庫基於從瀏覽器訪問或在數據庫中查詢的Web應用程序,但是對於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庫在播放穩定性或可擴展性/復雜性問題方面可能發揮最好的作用或接口最少? 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

src: http//nunoferro.pt/?p = 22

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM