ios客户端websocket的helloworld

ios8,xcode6 
https://github.com/square/SocketRocket  
https://github.com/killinux/SocketRocket  
中的一个文件夹SocketRocket,3包含三个文件 
SRWebSocket.h
SRWebSocket.m
SocketRocket-Prefix.pch
copy到工程中 
Java代码   收藏代码
  1. //  
  2. //  ViewController.h  
  3. //  TestWebs  
  4. //  
  5. //  Created by xiao7 on 14-10-9.  
  6. //  Copyright (c) 2014年 killinux. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "SocketRocket/SRWebSocket.h"  
  11.   
  12.   
  13. @interface ViewController : UIViewController<SRWebSocketDelegate>  
  14. {  
  15.     SRWebSocket *webSocket;  
  16.       
  17. }  
  18. @end  

Java代码  
  1. //  
  2. //  ViewController.m  
  3. //  TestWebs  
  4. //  
  5. //  Created by xiao7 on 14-10-9.  
  6. //  Copyright (c) 2014年 killinux. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12. @property (weak, nonatomic) IBOutlet UILabel *showTxt;  
  13.   
  14. @end  
  15.   
  16. @implementation ViewController  
  17.   
  18. - (void)viewDidLoad {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21.     webSocket.delegate = nil;  
  22.     [webSocket close];  
  23.     webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://192.168.0.102:8887"]]];  
  24.     webSocket.delegate = self;  
  25.     [webSocket open];  
  26.     NSLog(@"open success!");  
  27. }  
  28.   
  29. - (void)didReceiveMemoryWarning {  
  30.     [super didReceiveMemoryWarning];  
  31.     // Dispose of any resources that can be recreated.  
  32. }  
  33. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;  
  34. {  
  35.     NSLog(@"Websocket Connected");  
  36.     self.title = @"Connected!";  
  37. }  
  38.   
  39. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;  
  40. {  
  41.     NSLog(@":( Websocket Failed With Error %@", error);  
  42.     webSocket = nil;  
  43. }  
  44.   
  45. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;  
  46. {  
  47.     NSLog(@"Received \"%@\"", message);  
  48.     self.showTxt.text = message;  
  49. }  
  50.   
  51. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;  
  52. {  
  53.     NSLog(@"WebSocket closed");  
  54.     self.title = @"Connection Closed! (see logs)";  
  55.     webSocket = nil;  
  56. }  
  57.   
  58. @end  

引入4个库 
libicucore.dylib,CFNetwork.framework, Security.framework, Foundation.framework 



server的例子参考 
http://haoningabc.iteye.com/blog/2124605  

以上部分参考 
参考http://nonocast.cn/websocket-in-objective-c/ 
参考http://wenxin2009.iteye.com/blog/1707304 
有出入
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值