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"); }