forked from koalahl/PythonIniOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
executable file
·72 lines (55 loc) · 1.85 KB
/
Copy pathViewController.m
File metadata and controls
executable file
·72 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// ViewController.m
// Python-iOS-app
//
// Created by Fancyzero on 13-8-21.
// Copyright (c) 2013年 Fancyzero. All rights reserved.
//
#import "ViewController.h"
#import "AsyncSocket.h"
@interface ViewController ()
@property (nonatomic,strong)AsyncSocket * socket;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//必须在view appear之后,不能放在viewDidLoad中。具体原因可能因为server还未建立。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//创建socket对象
_socket = [[AsyncSocket alloc] initWithDelegate:self];
[_socket connectToHost:@"127.0.0.1" onPort:8000 withTimeout:-1 error:nil];
}
- (void)sendMessage
{
NSDictionary *dict = @{
@"from":@"local",
@"to":@"Server",
@"message":@"This is My book!"
};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
[_socket writeData:data withTimeout:-1 tag:100];
}
- (IBAction)SendMsg:(id)sender {
[self sendMessage];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
[_socket readDataWithTimeout:-1 tag:100];
NSLog(@"didConnectToHost");
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"from:%@ message:%@", dict[@"from"], dict[@"message"]);
[_socket readDataWithTimeout:-1 tag:100];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end