string NKSMSWebBrowserCallbackProtocol.didReceiveScriptMessageSync(string channel, string message)
 {
     if (msgHandlers.ContainsKey(channel))
     {
         var scriptHandler      = msgHandlers[channel];
         var body               = context.NKdeserialize(message);
         var result             = scriptHandler.didReceiveScriptMessageSync(new NKScriptMessage(channel, body));
         var retValueSerialized = context.NKserialize(result);
         return(retValueSerialized);
     }
     else
     {
         return("");
     }
 }
예제 #2
0
        // RENDERER PROCESS PIPE EVENT LOOP
        private async Task _processClientMessages()
        {
            var message = await readObjectAsync(syncPipeIn);

            if (message == null)
            {
                NKLogging.log("!Renderer Received Empty Message");
                cancelTokenSource.Cancel();
                return;
            }

            if (message.command == NKRemotingMessage.Command.NKScriptMessageSync)
            {
                var name = message.args[0];
                Dictionary <string, object> body = null;
                try
                {
                    body = context.NKdeserialize(message.args[1]) as Dictionary <string, object>;
                }
                catch (Exception ex)
                {
                    NKLogging.log("!Renderer Message Deserialization Error: " + ex.Message);
                }
                var nks = new NKScriptMessage(name, body);

                NKScriptMessageHandler handler = null;
                if (_localHandlers.ContainsKey(name))
                {
                    handler = _localHandlers[name];
                }
                else
                {
                    int target = Int32.Parse(body["$target"].ToString());
                    handler = NKScriptChannel.getNative(target);
                }
                if (handler != null)
                {
                    var nkr = new NKRemotingMessage();
                    nkr.command = NKRemotingMessage.Command.NKScriptMessageSyncReply;

                    try
                    {
                        var result = handler.didReceiveScriptMessageSync(nks);
                        nkr.args = new string[] { context.NKserialize(result) };
                    } catch (Exception ex)
                    {
                        NKLogging.log("!Renderer Message Processing Error: " + ex.Message);
                        NKLogging.log(ex.StackTrace);
                        nkr.args = new string[] { };
                    }
                    writeObject(syncPipeOut, nkr);
                }
                else
                {
                    NKLogging.log("+Renderer Received Unknown Script Message Sync");
                }
            }
            else if (message.command == NKRemotingMessage.Command.NKScriptMessage)
            {
                var name = message.args[0];
                Dictionary <string, object> body = null;
                try
                {
                    body = context.NKdeserialize(message.args[1]) as Dictionary <string, object>;
                }
                catch (Exception ex)
                {
                    NKLogging.log("!Renderer Message Deserialization Error: " + ex.Message);
                }
                var nks = new NKScriptMessage(name, body);

                NKScriptMessageHandler handler = null;
                if (_localHandlers.ContainsKey(name))
                {
                    handler = _localHandlers[name];
                }
                else
                {
                    int target = Int32.Parse(body["$target"].ToString());
                    handler = NKScriptChannel.getNative(target);
                }
                if (handler != null)
                {
                    handler.didReceiveScriptMessage(nks);
                }
                else
                {
                    NKLogging.log("+Renderer Received Unknown Script Message " + message.args[1]);
                }
            }
            else if (message.command == NKRemotingMessage.Command.NKRemotingClose)
            {
                try
                {
                    syncPipeIn.Close();
                    syncPipeOut.Close();
                    asyncPipe.Close();
                }
                catch { }

                Environment.Exit(0);
            }
            else if (message.command == NKRemotingMessage.Command.NKEvent)
            {
                var     eventType   = message.args[0];
                NKEvent eventObject = new NKEvent((IDictionary <string, object>)(NKData.jsonDeserialize(message.args[1])));
                NKEventEmitter.global.emit <NKEvent>(eventType, eventObject, false);
            }

            if (!cancelToken.IsCancellationRequested)
            {
                await _processClientMessages();
            }
            else
            {
                return;
            }
        }