单军华
2017-03-10 705dd5bda8a6cdbc97b65e9b046bdf49739cc87b
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
//
//  ViewController.m
//  SocketDemo
//
//  Created by Apple on 16/9/22.
//  Copyright © 2016年 Apple. All rights reserved.
//
 
#import "ViewController.h"
#import "Socket.h"
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self connectSocket];
}
 
-(void)connectSocket
{
    //接收新消息通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(insertMessage:) name:SocketNewMessageNotification object:nil];
    //socket断开通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(socketConnectError) name:SocketConnectErrorNotification object:nil];
    //socket连接通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(socketConnectSuccess) name:SocketConnectSuccessNotification object:nil];
    [Socket sharedInstance].socketHost = @"114.55.179.185";// host设定
    [Socket sharedInstance].socketPort = 6789;// port设定
    // 在连接前先进行手动断开
    [Socket sharedInstance].socket.userData = SocketOfflineByUser;
    [[Socket sharedInstance] cutOffSocket];
    // 确保断开后再连,如果对一个正处于连接状态的socket进行连接,会出现崩溃
    [[Socket sharedInstance] socketConnectHost:^{
    }];
    [Socket sharedInstance].socket.userData = SocketOfflineByServer;
}
 
-(void)insertMessage:(NSNotification*)noti
{
    
}
 
-(void)socketConnectSuccess
{
    
}
 
-(void)socketConnectError
{
    
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
@end