Skip to content

Commit 24c7a71

Browse files
committed
decouples event name from JSValue-backed block event handler
this is accomplished by MD5-hashing event names to correspond to the block values
1 parent cc608bb commit 24c7a71

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

SocketIO.xcodeproj/project.xcworkspace/xcshareddata/SocketIO.xccheckout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<string>https://github.com/MegaBits/SIOSocket.git</string>
1515
</dict>
1616
<key>IDESourceControlProjectPath</key>
17-
<string>SocketIO.xcodeproj/project.xcworkspace</string>
17+
<string>SocketIO.xcodeproj</string>
1818
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
1919
<dict>
2020
<key>0E77FB9618B0A7E05C2EE27348151B7941044E77</key>

SocketIO/SocketIO.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ - (void)testEmitMultiWord {
123123
[self waitForExpectationsWithTimeout: 10 handler: nil];
124124
}
125125

126+
- (void)testEmitMultiWordWithCharacters {
127+
XCTestExpectation *stringExpectation = [self expectationWithDescription: @"should emit multi word"];
128+
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket) {
129+
XCTAssertNotNil(socket, @"socket could not connect to localhost");
130+
[socket on: @"multi-word" callback: ^(SIOParameterArray *args) {
131+
NSString *response = [args firstObject];
132+
XCTAssert([response isEqualToString: @"word"], @"%@ != 'word'", response);
133+
134+
[stringExpectation fulfill];
135+
}];
136+
137+
[socket emit: @"multi-word"];
138+
}];
139+
140+
[self waitForExpectationsWithTimeout: 10 handler: nil];
141+
}
142+
126143
- (void)testBinaryData {
127144
XCTestExpectation *blobExpectation = [self expectationWithDescription: @"should send binary data as Blob"];
128145
[SIOSocket socketWithHost: @"http://localhost:3000" response: ^(SIOSocket *socket) {

SocketIO/Source/SIOSocket.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@
99
#import "SIOSocket.h"
1010
#import <JavaScriptCore/JavaScriptCore.h>
1111
#import "socket.io.js.h"
12+
#import <CommonCrypto/CommonCrypto.h>
1213

1314
#ifdef __IPHONE_8_0
1415
#import <WebKit/WebKit.h>
1516
#endif
1617

18+
static NSString *SIOMD5(NSString *string) {
19+
const char *cstr = [string UTF8String];
20+
unsigned char result[16];
21+
CC_MD5(cstr, (unsigned int)strlen(cstr), result);
22+
23+
return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
24+
result[0], result[1], result[2], result[3],
25+
result[4], result[5], result[6], result[7],
26+
result[8], result[9], result[10], result[11],
27+
result[12], result[13], result[14], result[15]
28+
];
29+
};
30+
1731
@interface SIOSocket ()
1832

1933
@property UIWebView *javascriptWebView;
@@ -121,7 +135,7 @@ - (JSContext *)javascriptContext {
121135

122136
// Event listeners
123137
- (void)on:(NSString *)event callback:(void (^)(SIOParameterArray *args))function {
124-
NSString *eventID = [event stringByReplacingOccurrencesOfString: @" " withString: @"_"];
138+
NSString *eventID = SIOMD5(event);
125139
self.javascriptContext[[NSString stringWithFormat: @"objc_%@", eventID]] = ^() {
126140
NSMutableArray *arguments = [NSMutableArray array];
127141
for (JSValue *object in [JSContext currentArguments]) {

socket_tester/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ server.on('connection', function(socket){
117117
socket.on('multi word', function() {
118118
socket.emit('multi word', 'word');
119119
});
120+
121+
socket.on('multi-word', function() {
122+
socket.emit('multi-word', 'word');
123+
});
120124
});

0 commit comments

Comments
 (0)