private void InitComm() { //Read _inServer = new SharedCommServer(false); //Write _outServer = new SharedCommServer(true); _inServer.InitComm(inCommFileSize, _inSharedCommFile); _outServer.InitComm(outCommFileSize, _outSharedCommFile); //开启读取线程 Action serverRead = () => { EventPacket ep; while (true) { ep = _inServer.GetMessage(); if (ep != null) { //把获取到的信息直接发送到浏览器中 Console.WriteLine("Message From Unity:" + ep.eventValue); _browser.ExecuteScriptAsync("DisplayValue", ep.eventValue); } //Console.WriteLine("serverRead!!"); //没那么频繁、可以调节 Thread.Sleep(200); } }; // ThreadPool.QueueUserWorkItem((o) => { serverWrite(); }); ThreadPool.QueueUserWorkItem((o) => { serverRead(); }); }
// Update is called once per frame void Update() { if (!_inServer.GetIsOpen()) { _inServer.Connect(_inSharedCommFile); } else { if (!isShowConnectOnce) { _outMessage.text = "连接成功!"; isShowConnectOnce = true; _image.gameObject.SetActive(true); } EventPacket ep = _inServer.GetMessage(); if (ep != null) { _outMessage.text = ep.eventValue; Debug.Log(ep.eventId + "," + ep.eventValue); } } if (!_outServer.GetIsOpen()) { _outServer.Connect(_outSharedCommFile); } }
public void CheckMessage() { _outCommServer.PushMessages(); EventPacket ep = _inCommServer.GetMessage(); if (ep != null) { HandleMessage(ep); } }
//Receiver public void CheckMessage() { if (Initialized) { try { // Ensure that no other threads try to use the stream at the same time. EventPacket ep = _inCommServer.GetMessage(); if (ep != null) { //main handlers if (ep.Type == BrowserEventType.Dialog) { DialogEvent dev = ep.Event as DialogEvent; if (dev != null) { if (OnJavaScriptDialog != null) { OnJavaScriptDialog(dev.Message, dev.DefaultPrompt, dev.Type); } } } if (ep.Type == BrowserEventType.Generic) { GenericEvent ge = ep.Event as GenericEvent; if (ge != null) { if (ge.Type == GenericEventType.JSQuery) { if (OnJavaScriptQuery != null) { OnJavaScriptQuery(ge.JsQuery); } } } if (ge.Type == GenericEventType.PageLoaded) { if (OnPageLoaded != null) { OnPageLoaded(ge.NavigateUrl); } } } } } catch (Exception e) { Debug.Log("Error reading from socket,waiting for plugin server to start..."); } } }
private void CheckMessage() { //push _outCommServer.PushMessages(); try { EventPacket ep = _inCommServer.GetMessage(); if (ep != null) { // if (ep.Type == BrowserEventType.Ping) // SendPing(); // // MessageBox.Show("PINGBACK"); if (ep.Type == BrowserEventType.Dialog) { DialogEvent dev = ep.Event as DialogEvent; switch (dev.Type) { case DialogEventType.Alert: { _inModalDialog = true; MessageBox.Show(dev.Message, "InApp"); SendDialogResponse(true); _inModalDialog = false; break; } case DialogEventType.Confirm: { _inModalDialog = true; if (MessageBox.Show(dev.Message, "InApp", MessageBoxButtons.OKCancel) == DialogResult.OK) { SendDialogResponse(true); } else { SendDialogResponse(false); } _inModalDialog = false; break; } } } if (ep.Type == BrowserEventType.Generic) { GenericEvent ge = ep.Event as GenericEvent; if (ge.Type == GenericEventType.JSQuery) { if (MessageBox.Show("JS QUERY:" + ge.StringContent, "Query", MessageBoxButtons.OKCancel) == DialogResult.OK) { SendQueryResponse("Query ok"); } else { SendQueryResponse("Query cancel"); } } if (ge.Type == GenericEventType.PageLoaded) { MessageBox.Show("Navigated to:" + ge.StringContent); } } } } catch (Exception e) { } }