Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Real time with Websocket

softoille@gmail.com edited this page Nov 29, 2019 · 1 revision

CefGlue Apps Only

Usage

Register server parameters in app start.

class Program
{
   static int Main(string[] args)
   {
      string startUrl = "local://app/chromely.html";

      ChromelyConfiguration config = ChromelyConfiguration
                                    .Create()
                                    ----
                                    .UseDefaultWebsocketHandler(address: string.Empty, port: 8181, onloadstartserver: true);

     var factory = WinapiHostFactory.Init();
     using (var window = factory.CreateWindow(() => new CefGlueBrowserHost(config),
           "chromely", constructionParams: new FrameWindowConstructionParams()))
      {
         ----
         return new EventLoop().Run(window);
     }
  }
}

- address - localhost. This can be empty if empty, it will use "localhost". Other addresses can be used.
- port - 8181.
- onloadstartserver if [true|false]. If true it will server will be started on application start.

Alternatively, server can also be started when application is running by implementing Controller actions that will start and stop the server. See example - WebsocketDemoController.

In the client (html)- implement normal HTML5 Websocket client:

<script type = "text/javascript">
         function WebSocketTest() {
            
            if ("WebSocket" in window) {
               alert("WebSocket is supported by your Browser!");
               
               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:8181/?name=connection-name");
				
               ws.onopen = function() {
               };
				
               ws.onmessage = function (evt) { 
               };
				
               ws.onclose = function() { 
               };
            } else {
            }
         }
      </script>

Please see CefGlue Demo for more options.

The Server

For real-time processing in applications, Chromely implements CefGlue's Websocket Cef CefServer.

The CefServer implementation can be found @ CefGlueServerHandler. While the server is configurable, the actual implementation is not. For developers who want to change the server implementation, they may have to download the source code and add their custom implementations.

Also Chromely is not tied to Cef CefServer. It can be replaced by other Websocket frameworks.

The Chromely Websocket Handler

To process messages from client to server and vice versa, an interface - IChromelyWebsocketHandler is provided. Although a default implementation - CefGlueWebsocketHandler - is provided, this is configurable and developers can provide their implementation and register it in the configuration object. ONLY one implementation is allowed. Multiple registrations will remove previously registered.

To register a handler we can either use default handler or provide a new one.

Use default:

  ----
  .UseDefaultWebsocketHandler(address: string.Empty, port: 8181, onloadstartserver: true);
  ----

Register new handler:

  ----
  .RegisterWebsocketHandler(address: string.Empty, port: 8181, onloadstartserver: true, sockeHandler: new NewCustomWebsocketHandler);
  ----

- NewCustomWebsocketHandler must implement IChromelyWebsocketHandler interface.

Message Processing

The Websocket message handling is customizable. The default handler - CefGlueWebsocketHandler allows for 4 types of message processing. The is categorized in the enum class MessageType.

Echo

Echo returns whatever is sent to the server to the client sending the data.

TargetRecipient

Sends data to a recipient client specified by connection name.

Broadcast

Sends data to all clients except the one sending it.

ControllerAction

Processes a Resfful controller action and returns the result.

Helper Classes

There are 2 static classes to help in server managing and message processing.

Clone this wiki locally