zoukankan      html  css  js  c++  java
  • iOS中WebSocket的使用

    https://github.com/square/SocketRocket

    简单使用如下

    1.初始化socket
    _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://echo.websocket.org"]]];
        
        _webSocket.delegate = self;
        [_webSocket open];
    2设置一些协议
    #pragma mark - SRWebSocketDelegate
    - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
    {
        NSLog(@"Websocket Connected");
    }
    
    - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
    {
        NSLog(@":( Websocket Failed With Error %@", error);
        
        _webSocket = nil;
    }
    
    - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
    {
        NSLog(@"Received "%@"", message);
        
        if ([self.delegate respondsToSelector:@selector(recivedWebSocketMessage:)]) {
            [self.delegate recivedWebSocketMessage:message];
        }
    
    }
    
    - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
    {
        NSLog(@"WebSocket closed");
        _webSocket = nil;
    }
    
    - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
    {
        NSLog(@"Websocket received pong");
    }
    
  • 相关阅读:
    Spring spEL
    Spring 使用外部部署文件
    Spring 自动装配
    spring 属性配置细节
    hdu 1054 Strategic Game
    fzu 2037 Maximum Value Problem
    将博客搬至CSDN
    HDU 4714 Tree2Cycle
    HDU 1009 The Shortest Path in Nya Graph
    POJ 1942 Paths on a Grid 组合数的优化
  • 原文地址:https://www.cnblogs.com/keyan1102/p/5361201.html
Copyright © 2011-2022 走看看