简体   繁体   English

JavaScriptCore + Socket.IO

[英]JavaScriptCore + Socket.IO

I'm trying to connect to a Node.JS server with an iOS App, but instead of using a 3rd party native library, I'm trying to connect it with the JavaScript Socket.IO client & to pass the data that I'm receiving to my native code. 我正在尝试使用iOS App连接到Node.JS服务器,但是我没有使用第三方的本地库,而是尝试将其与JavaScript Socket.IO客户端连接并传递我收到我的本机代码。 I'm loading the Socket.IO module into the JSContext & it loads well, I can see all the available objects/functions inside the io object. 我正在将Socket.IO模块加载到JSContext中,并且加载得很好,我可以看到io对象中所有可用的对象/函数。

But when it reaches the line for connecting to my Node.JS server the execution of my JavaScript code stopes & I cannot find out why. 但是,当它到达连接到我的Node.JS服务器的行时,我的JavaScript代码就会停止执行,并且我无法找出原因。

If I pause the app I don't see any additional threads for opening connections. 如果我暂停应用程序,则看不到任何其他用于打开连接的线程。 Is it possible that JavaScriptCore doesn't have support for web sockets? JavaScriptCore是否可能不支持Web套接字?

My Objective-C code: 我的Objective-C代码:

JSContext *context = [JSContext new];

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"socket.io" ofType:@"js"];
NSString *socketIO = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];;


filePath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"js"];
NSString *client = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
context[@"socket_url"] = @"http://192.168.1.128:8080";
context[@"log"] = ^(NSString *string){

    NSLog(@"JavaScript:\n\t%@", string);
};
context[@"iOSBlock"] = ^(id deviceState) {

    id JSON = [NSJSONSerialization JSONObjectWithData:deviceState options:NSJSONReadingMutableLeaves error:nil];
    // I'll do something here if I get reach this part :)
};

[context evaluateScript:socketIO];
[context evaluateScript:client];

My JavaScript code: 我的JavaScript代码:

var socket = io.connect(socket_url);

socket.on('connect', function(){
      log("Connected!");
});

socket.on('device-change', iOSBlock(deviceState)); 

JavaScriptCore itself only implements the ECMAScript spec . JavaScriptCore本身仅实现ECMAScript规范 Stuff like XMLHTTPRequest, web sockets, DOM, etc. comes from WebKit. XMLHTTPRequest,Web套接字,DOM等之类的东西都来自WebKit。 If you absolutely want to run the Socket.IO JavaScript client, you can perhaps use a hidden UIWebView, although I'd recommend using Socket.IO-objc . 如果您绝对希望运行Socket.IO JavaScript客户端,则尽管我建议使用Socket.IO-objc ,也可以使用隐藏的UIWebView。

On an unrelated note, you can see what errors JSContext generates by either inspecting its exception property, or by setting a block that prints the error to the exceptionHandler property: 在不相关的注释上,您可以通过检查JSContext的exception属性,或通过设置将错误输出到exceptionHandler属性的块,来查看JSContext生成了哪些错误:

context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
    NSLog(@"[%@:%@:%@] %@\n%@", exception[@"sourceURL"], exception[@"line"], exception[@"column"], exception, [exception[@"stack"] toObject]);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM