-
Notifications
You must be signed in to change notification settings - Fork 2
Real time with Websocket
CefGlue Apps Only
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.
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.
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.
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 returns whatever is sent to the server to the client sending the data.
Sends data to a recipient client specified by connection name.
Sends data to all clients except the one sending it.
Processes a Resfful controller action and returns the result.
There are 2 static classes to help in server managing and message processing.
Chromely
Getting Started
The Basics
Digging Deeper
- Sub-Process
- Infrastructure
- Restful Resources
- Register Resource Assemblies
- Custom Local Resource Handling
- Custom Scheme Handling
- Expose .NET class to JavaScript
- Generic Message Routing
- Real-time with Websocket
- External Url Registration
Angular - React - Vue