ios客户端websocket的helloworld
2015-08-20 14:32
441 查看
ios8,xcode6
https://github.com/square/SocketRocket
https://github.com/killinux/SocketRocket
中的一个文件夹SocketRocket,3包含三个文件
SRWebSocket.h
SRWebSocket.m
SocketRocket-Prefix.pch
copy到工程中
Java代码
//
// ViewController.h
// TestWebs
//
// Created by xiao7 on 14-10-9.
// Copyright (c) 2014年 killinux. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SocketRocket/SRWebSocket.h"
@interface ViewController : UIViewController<SRWebSocketDelegate>
{
SRWebSocket *webSocket;
}
@end
Java代码
//
// ViewController.m
// TestWebs
//
// Created by xiao7 on 14-10-9.
// Copyright (c) 2014年 killinux. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *showTxt;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webSocket.delegate = nil;
[webSocket close];
webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://192.168.0.102:8887"]]];
webSocket.delegate = self;
[webSocket open];
NSLog(@"open success!");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
{
NSLog(@"Websocket Connected");
self.title = @"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);
self.showTxt.text = message;
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
{
NSLog(@"WebSocket closed");
self.title = @"Connection Closed! (see logs)";
webSocket = nil;
}
@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
有出入
https://github.com/square/SocketRocket
https://github.com/killinux/SocketRocket
中的一个文件夹SocketRocket,3包含三个文件
SRWebSocket.h
SRWebSocket.m
SocketRocket-Prefix.pch
copy到工程中
Java代码
//
// ViewController.h
// TestWebs
//
// Created by xiao7 on 14-10-9.
// Copyright (c) 2014年 killinux. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SocketRocket/SRWebSocket.h"
@interface ViewController : UIViewController<SRWebSocketDelegate>
{
SRWebSocket *webSocket;
}
@end
Java代码
//
// ViewController.m
// TestWebs
//
// Created by xiao7 on 14-10-9.
// Copyright (c) 2014年 killinux. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *showTxt;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webSocket.delegate = nil;
[webSocket close];
webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://192.168.0.102:8887"]]];
webSocket.delegate = self;
[webSocket open];
NSLog(@"open success!");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
{
NSLog(@"Websocket Connected");
self.title = @"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);
self.showTxt.text = message;
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
{
NSLog(@"WebSocket closed");
self.title = @"Connection Closed! (see logs)";
webSocket = nil;
}
@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
有出入
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- php使用websocket示例详解
- php+html5基于websocket实现聊天室的方法
- Javascript WebSocket使用实例介绍(简明入门教程)
- java中实现兼容ie6 7 8 9的spring4+websocket
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音