/
ConnectViewController.m
80 lines (67 loc) · 2.49 KB
/
ConnectViewController.m
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
73
74
75
76
77
78
79
80
//
// FirstViewController.m
// MDWampDemo
//
// Created by Niko Usai on 27/06/14.
// Copyright (c) 2014 mogui.it. All rights reserved.
//
#import "ConnectViewController.h"
@interface ConnectViewController () <UITextFieldDelegate, MDWampClientDelegate>
@property (assign) BOOL connected;
@end
@implementation ConnectViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (self.connected) {
return NO;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)connect:(id)sender {
// CHECK empty fields
if (!self.connected) {
MDWampTransportWebSocket *transport = [[MDWampTransportWebSocket alloc] initWithServer:[NSURL URLWithString:self.hostField.text] protocolVersions:@[kMDWampProtocolWamp2json]];
// Test Raw socket
// MDWampTransportRawSocket *transport = [[MDWampTransportRawSocket alloc] initWithHost:@"127.0.0.1" port:9000];
// [transport setSerialization:kMDWampSerializationJSON];
//
MDWamp *ws = [[MDWamp alloc] initWithTransport:transport realm:self.realmField.text delegate:self];
[AppDel setWampConnection:ws];
[ws connect];
} else {
MDWamp *ws = [AppDel wampConnection];
[ws disconnect];
}
}
- (void) mdwamp:(MDWamp*)wamp sessionEstablished:(NSDictionary*)info {
self.connected = YES;
[self.connectButton setTitle:@"DISCONNECT" forState:UIControlStateNormal];
NSLog(@"serverd details: %@", info);
[self.connectIcon setHighlighted:YES];
self.serverDetails.text = [NSString stringWithFormat:@"\n"\
@"authid:\t %@\n"\
@"authmethod:\t %@\n"\
@"authrole:\t %@\n"\
@"roles:\t %@\n", info[@"authid"], info[@"authmethod"], info[@"authrole"], [[info[@"roles"] allKeys] componentsJoinedByString:@", "]];
}
- (void) mdwamp:(MDWamp *)wamp closedSession:(NSInteger)code reason:(NSString*)reason details:(NSDictionary *)details {
[self.connectIcon setHighlighted:NO];
self.connected = NO;
self.serverDetails.text = reason;
[self.connectButton setTitle:@"CONNECT" forState:UIControlStateNormal];
}
@end