OC版websocket地址swift版webSocket地址
oc版webSocket的使用
- 初始化webSocket
NSString *websocketPath = @”ws:address:port”;
self.srwebsocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:websocketPath]]];
self.srwebsocket.delegate = self;
[self.srwebsocket open]; webSocket代理方法
//webSocket连接成功
-(void)webSocketDidOpen:(SRWebSocket *)srWebSocket{}//webSocket连接失败
-(void)webSocket:(SRWebSocket *)srWebSocket didFailWithError:(NSError *)error{}//webSocket收到服务器发过来的消息
-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{}客户端向服务器发送消息
[self.srWebSocket send:@””];
swift版webSocket的使用
- 初始化webSocket
webSocket = WebSocket(url: NSURL(scheme: “ws”, host: GHost, path: “/ws_eventbus”)!)
webSocket.delegate = self
webSocket.connect() webSocket代理方法
// webSocket连接成功
func websocketDidConnect(socket: WebSocket) {}// webScoket连接失败
func websocketDidDisconnect(socket: WebSocket, error: NSError?) {}// webSocket收到服务器发来的消息
func websocketDidReceiveMessage(socket: WebSocket, text: String) {}客户端向服务端发送消息
webSocket.writeString(“”)