I am pleased to inform you of the release of this extension that adds WebSocket support to CSHTML5.
It requires CSHTML5 v1.0 Beta 7.2 or newer.
To use, simply add a new class named "WebSocket.cs" to your project, and copy/paste the following code:
Code: Select all
using System;
//------------------------------------
// This extension adds WebSocket support to C#/XAML for HTML5 (www.cshtml5.com)
//
// It requires Beta 7.2 or newer.
//
// This project is licensed under The open-source MIT license:
// https://opensource.org/licenses/MIT
//
// Copyright 2016 Userware / CSHTML5
//------------------------------------
namespace CSHTML5.Extensions.WebSockets
{
public class WebSocket
{
object _referenceToTheJavaScriptWebSocketObject;
public event EventHandler OnOpen;
public event EventHandler OnClose;
public event EventHandler<OnMessageEventArgs> OnMessage;
public event EventHandler<OnErrorEventArgs> OnError;
public WebSocket(string uri)
{
_referenceToTheJavaScriptWebSocketObject = CSHTML5.Interop.ExecuteJavaScript("new WebSocket($0)", uri);
CSHTML5.Interop.ExecuteJavaScript(
@"$0.onopen = $1;
$0.onclose = $2;
$0.onmessage = $3;
$0.onerror = $4",
_referenceToTheJavaScriptWebSocketObject,
(Action<object>)this.OnOpenCallback,
(Action<object>)this.OnCloseCallback,
(Action<object>)this.OnMessageCallback,
(Action<object>)this.OnErrorCallback);
}
public void Send(string message)
{
CSHTML5.Interop.ExecuteJavaScript("$0.send($1)", _referenceToTheJavaScriptWebSocketObject, message);
}
public void Close()
{
CSHTML5.Interop.ExecuteJavaScript("$0.close()", _referenceToTheJavaScriptWebSocketObject);
}
void OnOpenCallback(object e)
{
if (this.OnOpen != null)
OnOpen(this, new EventArgs());
}
void OnCloseCallback(object e)
{
if (this.OnClose != null)
OnClose(this, new EventArgs());
}
void OnMessageCallback(object e)
{
string data = string.Empty;
if (!Convert.ToBoolean(CSHTML5.Interop.ExecuteJavaScript("(typeof $0 === 'undefined')", e)))
data = Convert.ToString(CSHTML5.Interop.ExecuteJavaScript("$0.data", e));
if (this.OnMessage != null)
OnMessage(this, new OnMessageEventArgs(data));
}
void OnErrorCallback(object e)
{
string data = string.Empty;
if (!Convert.ToBoolean(CSHTML5.Interop.ExecuteJavaScript("(typeof $0 === 'undefined')", e)))
data = Convert.ToString(CSHTML5.Interop.ExecuteJavaScript("$0.data", e));
if (this.OnError != null)
OnError(this, new OnErrorEventArgs(data));
}
public ReadyState ReadyState
{
get
{
int readyStateInt = Convert.ToInt32(CSHTML5.Interop.ExecuteJavaScript(@"$0.readyState", _referenceToTheJavaScriptWebSocketObject));
return (ReadyState)readyStateInt;
}
}
}
public class OnMessageEventArgs : EventArgs
{
public readonly string Data;
public OnMessageEventArgs(string data)
{
this.Data = data;
}
}
public class OnErrorEventArgs : EventArgs
{
public readonly string Data;
public OnErrorEventArgs(string data)
{
this.Data = data;
}
}
public enum ReadyState : int
{
/// <summary>
/// The connection is not yet open.
/// </summary>
CONNECTING = 0,
/// <summary>
/// The connection is open and ready to communicate.
/// </summary>
OPEN = 1,
/// <summary>
/// The connection is in the process of closing.
/// </summary>
CLOSING = 2,
/// <summary>
/// The connection is closed or couldn't be opened.
/// </summary>
CLOSED = 3
}
}
The extension works by wrapping the JavaScript "WebSocket" class into a C#/XAML class for consumption by CSHTML5-based apps.
You can test the code by calling the following C# method:
Code: Select all
void TestWebSockets()
{
//------------------------------------------------
// This test calls the "Echo" service provided by http://www.websocket.org/echo.html
//------------------------------------------------
var webSocket = new CSHTML5.Extensions.WebSockets.WebSocket("ws://echo.websocket.org/");
webSocket.OnMessage += (s, e) => MessageBox.Show("RESPONSE: " + e.Data);
webSocket.OnError += (s, e) => MessageBox.Show("ERROR: " + e.Data);
webSocket.OnClose += (s, e) => MessageBox.Show("WebSocket Closed");
webSocket.OnOpen += (s,e) =>
{
MessageBox.Show("WebSocket Opened");
// Send a message to the Echo service, which will respond with the same message:
webSocket.Send("CSHTML5 rocks!");
};
}
Regards,
JS-Support