WebSocket
Introduction
Handle.GET(8080, "/wstest", (Request req) =>
{
// Checking if its a WebSocket upgrade request.
if (req.WebSocketUpgrade)
{
// Setting some headers and cookies on response for WebSockets upgrade.
List<String> myCookies = new List<String>
{
"MyCookie1=123", "MyCookie2=456"
};
var myHeaders = new Dictionary<String, String>()
{
{ "MyHeader", "MyHeaderData" }
};
// Performing upgrade and getting WebSocket object
// (SendUpgrade call implicitly sends an HTTP response confirming
// WebSocket upgrade, so another response can't be returned
// in this handler).
WebSocket ws = req.SendUpgrade("echotestws", myCookies, myHeaders);
// Immediately sending a message on the obtained WebSocket.
ws.Send("Hello WebSocket!");
// Sending another message on the WebSocket.
ws.Send("Hello again WebSocket!");
// Indicating that response on the original request was already
// sent (during SendUpgrade call).
return HandlerStatus.Handled;
}
// We only support WebSockets upgrades in this HTTP handler
// and not other ordinary HTTP requests.
return new Response()
{
StatusCode = 500,
StatusDescription = "WebSocket upgrade on " + req.Uri + " was not approved."
};
});Details of the SendUpgrade Call
WebSocket Group Name and Disconnect Handlers
WebSocket Object
Operating on Multiple WebSockets
Additional Information
Last updated