/// <summary> /// Called by the browser to handle the postMessage() call in Javascript. /// |var_message| is expected to be a string that contains the name of the /// method to call. Note that the setFrequency method takes a single /// parameter, the frequency. The frequency parameter is encoded as a string /// and appended to the 'setFrequency' method name after a ':'. Examples /// of possible message strings are: /// playSound /// stopSound /// setFrequency:880 /// If |var_message| is not a recognized method name, this method does nothing. /// </summary> /// <param name="varMessage"></param> private void OnHandleMessage(object sender, Var varMessage) { if (!varMessage.IsString) { return; } var message = varMessage.AsString(); if (message == PLAY_SOUND_ID) { audio.StartPlayback(); } else if (message == STOP_SOUND_ID) { audio.StopPlayback(); } else if (message.Contains(SET_FREQUENCY_ID)) { // The argument to setFrequency is everything after the first ':'. var sep_pos = message.IndexOf(MESSAGE_ARGUMENT_SEPARATOR); if (sep_pos > 0) { var stringArg = message.Substring(sep_pos + 1); // Got the argument value as a string: try to convert it to a number. double doubleValue = 0; if (Double.TryParse(stringArg, out doubleValue)) { Frequency = doubleValue; return; } } } }
// Called by the browser to handle the postMessage() call in Javascript. // The message in this case is expected to contain the string 'getUrl' // followed by a ':' separator, then the URL to fetch. If a valid message // of the form 'getUrl:URL' is received, then start up an asynchronous // download of URL. In the event that errors occur, this method posts an // error string back to the browser. private void OnHandleMessage(object sender, Var varMsg) { LogToConsole(PPLogLevel.Log, varMsg); if (!varMsg.IsString) { return; } var message = varMsg.AsString(); if (message.StartsWith(LOAD_URL_METHOD_ID)) { // The argument to getUrl is everything after the first ':'. var sepPos = message.IndexOf(messageArgumentSeparator); if (sepPos >= 0) { var url = message.Substring(sepPos + 1); Console.WriteLine($"URL_LoaderInstance HandleMessage ({message}, {url})"); try { var handler = new URL_LoaderHandler(this, url); // Starts asynchronous download. When download is finished or when an // error occurs, |handler| posts the results back to the browser // vis PostMessage and self-destroys. handler.Start(); } catch (Exception exc) { LogToConsoleWithSource(PPLogLevel.Error, exc.Source, exc.Message); } } } }
private void OnHandleMessage(object sender, Var varMessage) { if (!varMessage.IsString) { return; } var message = varMessage.AsString(); // This message must contain a command character followed by ';' and // arguments like "X;arguments". if (message.Length < 2 || message[1] != ';') { return; } switch (message[0]) { case MSG_CREATE_UDP: // The command 'b' requests to create a UDP connection the // specified HOST. // HOST is passed as an argument like "t;HOST". Connect(message.Substring(2), false); break; case MSG_CREATE_TCP: // The command 'o' requests to connect to the specified HOST. // HOST is passed as an argument like "u;HOST". Connect(message.Substring(2), true); break; case MSG_CLOSE: // The command 'c' requests to close without any argument like "c;" Close(); break; case MSG_LISTEN: // The command 'l' starts a listening socket (server). short port = 0; short.TryParse(message.Substring(2), out port); echoServer = new EchoServer(this, port); break; case MSG_SEND: // The command 't' requests to send a message as a text frame. The // message passed as an argument like "t;message". Send(message.Substring(2)); break; default: var status = $"Unhandled message from JavaScript: {message}"; PostMessage(status); break; } }
static void Main(string[] args) { var tail = VarList <char> .EmptyList; var text = new Var <VarList <char> >(); var output = new Var <VarList <char> >(); foreach (var result in SimpleSentence().BuildQuery(text, tail) & Capitalise(text, output)) { Console.WriteLine(output.AsString()); } Console.ReadLine(); }
private async void OnReceiveMessage(object sender, Var var_message) { if (!var_message.IsString) { return; } var message = var_message.AsString(); // This message must contain a command character followed by ';' and // arguments like "X;arguments". if (message.Length < 2 || message[1] != ';') { return; } switch (message[0]) { case 'o': // The command 'o' requests to open the specified URL. // URL is passed as an argument like "o;URL". if (IsUsingAsync) { await OpenAsync(message.Substring(2)); } else { Open(message.Substring(2)); } break; case 'c': // The command 'c' requests to close without any argument like "c;" if (IsUsingAsync) { await CloseAsync(); } else { Close(); } break; case 'b': // The command 'b' requests to send a message as a binary frame. The // message is passed as an argument like "b;message". //Send(message.Substring(2), WebSocketMessageType.Binary); if (IsUsingAsync) { await SendAsync(message.Substring(2), WebSocketMessageType.Binary); } else { Send(message.Substring(2), WebSocketMessageType.Binary); } break; case 't': // The command 't' requests to send a message as a text frame. The message // is passed as an argument like "t;message". if (IsUsingAsync) { await SendAsync(message.Substring(2), WebSocketMessageType.Text); } else { Send(message.Substring(2), WebSocketMessageType.Text); } break; case 'a': // The command 'a' requests that we use asynchronous message handling if (IsConnected()) { PostMessage("You must close and reopen the connection to change to asynchronouse message handling."); } else { PostMessage("Using asynchronous message handling."); IsUsingAsync = true; } break; case 's': // The command 's' requests to we use synchronous message handling if (IsConnected()) { PostMessage("You must close and reopen the connection to change to synchronouse message handling."); } else { PostMessage("Using synchronouse message handling"); IsUsingAsync = false; } break; } }