Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/MegaBits/SIOSocket into t…
Browse files Browse the repository at this point in the history
…hread-safety
  • Loading branch information
pcperini committed Nov 5, 2014
2 parents d4b8813 + 24c7a71 commit ca8a1cd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ then...
}];
```
A full demo can be found over at [MegaBits/WorldPin](https://github.com/MegaBits/WorldPin) (currently still requires SIOSocket v0.2.0)
or, in Swift...
```swift
// ...
SIOSocket.socketWithHost("http://localhost:3000") { (socket: SIOSocket) in
self.socket = socket
}
```

A full demo can be found over at [MegaBits/WorldPin](https://github.com/MegaBits/WorldPin)

## Types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<string>https://github.com/MegaBits/SIOSocket.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SocketIO.xcodeproj/project.xcworkspace</string>
<string>SocketIO.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>0E77FB9618B0A7E05C2EE27348151B7941044E77</key>
Expand Down
17 changes: 17 additions & 0 deletions SocketIO/SocketIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ - (void)testEmitMultiWord {
[self waitForExpectationsWithTimeout: 10 handler: nil];
}

- (void)testEmitMultiWordWithCharacters {
XCTestExpectation *stringExpectation = [self expectationWithDescription: @"should emit multi word"];
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket) {
XCTAssertNotNil(socket, @"socket could not connect to localhost");
[socket on: @"multi-word" callback: ^(SIOParameterArray *args) {
NSString *response = [args firstObject];
XCTAssert([response isEqualToString: @"word"], @"%@ != 'word'", response);

[stringExpectation fulfill];
}];

[socket emit: @"multi-word"];
}];

[self waitForExpectationsWithTimeout: 10 handler: nil];
}

- (void)testBinaryData {
XCTestExpectation *blobExpectation = [self expectationWithDescription: @"should send binary data as Blob"];
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket) {
Expand Down
16 changes: 15 additions & 1 deletion SocketIO/Source/SIOSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
#import "SIOSocket.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "socket.io.js.h"
#import <CommonCrypto/CommonCrypto.h>

#ifdef __IPHONE_8_0
#import <WebKit/WebKit.h>
#endif

static NSString *SIOMD5(NSString *string) {
const char *cstr = [string UTF8String];
unsigned char result[16];
CC_MD5(cstr, (unsigned int)strlen(cstr), result);

return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
};

@interface SIOSocket ()

@property UIWebView *javascriptWebView;
Expand Down Expand Up @@ -121,7 +135,7 @@ - (JSContext *)javascriptContext {

// Event listeners
- (void)on:(NSString *)event callback:(void (^)(SIOParameterArray *args))function {
NSString *eventID = [event stringByReplacingOccurrencesOfString: @" " withString: @"_"];
NSString *eventID = SIOMD5(event);
self.javascriptContext[[NSString stringWithFormat: @"objc_%@", eventID]] = ^() {
NSMutableArray *arguments = [NSMutableArray array];
for (JSValue *object in [JSContext currentArguments]) {
Expand Down
4 changes: 4 additions & 0 deletions socket_tester/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,8 @@ server.on('connection', function(socket){
socket.on('multi word', function() {
socket.emit('multi word', 'word');
});

socket.on('multi-word', function() {
socket.emit('multi-word', 'word');
});
});

0 comments on commit ca8a1cd

Please sign in to comment.