`
haoningabc
  • 浏览: 1498242 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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到工程中
//
//  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

//
//  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
有出入
  • 大小: 86.2 KB
分享到:
评论
1 楼 XieYupeng520 2015-04-08  
  您好,我用了这个方法,发现得不到一样的效果,主要体现为didMessage没打印,然后我去Socket源代码里追踪发现,设好的delegate不知为何到后面变成了空!I need some help, and can you help me?qq:806622085

相关推荐

    快速实现Uniapp websocket实时通讯, 兼容NVUE和VUE,支持厂商通道推送

    提到的“GoEasyDemo-Uniapp-Helloworld-2.4”是本指南或者示例代码的名称,它可能是一个基础的演示项目,用来展示如何在Uniapp中使用WebSocket进行实时通讯。虽然具体的代码实现细节不在本文档讨论范围内,但可以...

    用Swift写服务端 — Perfect框架学习

    try response.write("Hello, World!") } server.addRoute(method: .get, uri: "/hello", target: handleRequest) try server.start() ``` 此外,Perfect还支持多种数据库连接,如SQLite、MySQL、PostgreSQL等。你...

    cocos2d-x 聊天小程序1 数据的简单数据通信

    request-&gt;setRequestData("message=Hello%20World", "application/x-www-form-urlencoded"); httpClient-&gt;send(request); request-&gt;release(); ``` 2. **WebSocket通信**:对于实时聊天应用,WebSocket是一个更好的...

    ChatRoomSSH.zip

    socket.emit("chat message", "Hello, world!") ``` 如果需要实现实时的私聊功能,我们可以在`emit`时指定接收方的用户ID,服务器端负责将消息转发给指定的用户。 此外,考虑到项目可能需要集成到已有的应用中,...

    Hprose_for_HTML5用户手册.pdf

    client.invoke('hello', ['world'], function(result) { console.log('服务器返回:' + result); }); ``` ```java // 服务器端示例 (Java) import hprose.server.HproseHttpServer; public class Server { public ...

    QT 手机开发培训教程

    2. WebSockets:学习如何使用QT实现双向通信的WebSocket客户端和服务器。 六、数据库访问 1. SQL数据库支持:QT支持SQLite、MySQL、PostgreSQL等多种数据库,讲解如何进行数据库连接、查询、事务处理。 七、QT...

    Xamarin Signalr入门例子

    在`HubProxy`上,你可以调用服务器方法,例如`hubProxy.Invoke("Send", "User1", "Hello, world!");`来向服务器发送消息。同时,通过订阅的事件,接收服务器返回的数据。 ### 六、错误处理和断线重连 SignalR提供...

Global site tag (gtag.js) - Google Analytics