Modified a few for Example3
This commit is contained in:
parent
ca4fdabb2e
commit
74ec969758
@ -24,4 +24,3 @@ using System.Runtime.CompilerServices;
|
|||||||
|
|
||||||
//[assembly: AssemblyDelaySign(false)]
|
//[assembly: AssemblyDelaySign(false)]
|
||||||
//[assembly: AssemblyKeyFile("")]
|
//[assembly: AssemblyKeyFile("")]
|
||||||
|
|
||||||
|
@ -7,10 +7,9 @@ namespace Example3
|
|||||||
{
|
{
|
||||||
public class Chat : WebSocketBehavior
|
public class Chat : WebSocketBehavior
|
||||||
{
|
{
|
||||||
private static int _num = 0;
|
private string _name;
|
||||||
|
private static int _number = 0;
|
||||||
private string _name;
|
private string _prefix;
|
||||||
private string _prefix;
|
|
||||||
|
|
||||||
public Chat ()
|
public Chat ()
|
||||||
: this (null)
|
: this (null)
|
||||||
@ -24,13 +23,15 @@ namespace Example3
|
|||||||
|
|
||||||
private string getName ()
|
private string getName ()
|
||||||
{
|
{
|
||||||
var name = Context.QueryString ["name"];
|
var name = Context.QueryString["name"];
|
||||||
return !name.IsNullOrEmpty () ? name : (_prefix + getNum ());
|
return !name.IsNullOrEmpty ()
|
||||||
|
? name
|
||||||
|
: (_prefix + getNumber ());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getNum ()
|
private static int getNumber ()
|
||||||
{
|
{
|
||||||
return Interlocked.Increment (ref _num);
|
return Interlocked.Increment (ref _number);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnOpen ()
|
protected override void OnOpen ()
|
||||||
|
@ -8,11 +8,8 @@ namespace Example3
|
|||||||
{
|
{
|
||||||
protected override void OnMessage (MessageEventArgs e)
|
protected override void OnMessage (MessageEventArgs e)
|
||||||
{
|
{
|
||||||
var name = Context.QueryString ["name"];
|
var name = Context.QueryString["name"];
|
||||||
var msg = !name.IsNullOrEmpty ()
|
var msg = !name.IsNullOrEmpty () ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data;
|
||||||
? String.Format ("'{0}' to {1}", e.Data, name)
|
|
||||||
: e.Data;
|
|
||||||
|
|
||||||
Send (msg);
|
Send (msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using System.Text;
|
||||||
using WebSocketSharp;
|
using WebSocketSharp;
|
||||||
using WebSocketSharp.Net;
|
using WebSocketSharp.Net;
|
||||||
using WebSocketSharp.Server;
|
using WebSocketSharp.Server;
|
||||||
@ -9,26 +10,32 @@ namespace Example3
|
|||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private static HttpServer _httpsv;
|
public static void Main (string[] args)
|
||||||
|
|
||||||
public static void Main (string [] args)
|
|
||||||
{
|
{
|
||||||
_httpsv = new HttpServer (4649);
|
/* Create a new instance of the HttpServer class.
|
||||||
//_httpsv = new HttpServer (4649, true); // For Secure Connection
|
*
|
||||||
|
* If you would like to provide the secure connection, you should create the instance
|
||||||
|
* with the 'secure' parameter set to true.
|
||||||
|
*/
|
||||||
|
var httpsv = new HttpServer (4649);
|
||||||
|
//httpsv = new HttpServer (4649, true);
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// Changing the logging level
|
// To change the logging level.
|
||||||
_httpsv.Log.Level = LogLevel.Trace;
|
httpsv.Log.Level = LogLevel.Trace;
|
||||||
|
|
||||||
|
// To change the wait time for the response to the WebSocket Ping or Close.
|
||||||
|
httpsv.WaitTime = TimeSpan.FromSeconds (2);
|
||||||
#endif
|
#endif
|
||||||
/* For Secure Connection
|
/* To provide the secure connection.
|
||||||
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
|
||||||
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
var password = ConfigurationManager.AppSettings["CertFilePassword"];
|
||||||
_httpsv.Certificate = new X509Certificate2 (cert, password);
|
httpsv.Certificate = new X509Certificate2 (cert, password);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* For HTTP Authentication (Basic/Digest)
|
/* To provide the HTTP Authentication (Basic/Digest).
|
||||||
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
||||||
_httpsv.Realm = "WebSocket Test";
|
httpsv.Realm = "WebSocket Test";
|
||||||
_httpsv.UserCredentialsFinder = identity => {
|
httpsv.UserCredentialsFinder = identity => {
|
||||||
var expected = "nobita";
|
var expected = "nobita";
|
||||||
return identity.Name == expected
|
return identity.Name == expected
|
||||||
? new NetworkCredential (expected, "password", "gunfighter")
|
? new NetworkCredential (expected, "password", "gunfighter")
|
||||||
@ -36,32 +43,52 @@ namespace Example3
|
|||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Not to remove inactive clients in WebSocket services periodically
|
// To set the document root path.
|
||||||
//_httpsv.KeepClean = false;
|
httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"];
|
||||||
|
|
||||||
// Setting the document root path
|
// To set the HTTP GET method event.
|
||||||
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
httpsv.OnGet += (sender, e) => {
|
||||||
|
var req = e.Request;
|
||||||
|
var res = e.Response;
|
||||||
|
|
||||||
// Setting HTTP method events
|
var path = req.RawUrl;
|
||||||
_httpsv.OnGet += (sender, e) => onGet (e);
|
if (path == "/")
|
||||||
|
path += "index.html";
|
||||||
|
|
||||||
// Adding WebSocket services
|
var content = httpsv.GetFile (path);
|
||||||
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
if (content == null) {
|
||||||
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
res.StatusCode = (int) HttpStatusCode.NotFound;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* With initializing
|
if (path.EndsWith (".html")) {
|
||||||
_httpsv.AddWebSocketService<Chat> (
|
res.ContentType = "text/html";
|
||||||
|
res.ContentEncoding = Encoding.UTF8;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.WriteContent (content);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Not to remove the inactive WebSocket sessions periodically.
|
||||||
|
//httpsv.KeepClean = false;
|
||||||
|
|
||||||
|
// Add the WebSocket services.
|
||||||
|
httpsv.AddWebSocketService<Echo> ("/Echo");
|
||||||
|
httpsv.AddWebSocketService<Chat> ("/Chat");
|
||||||
|
|
||||||
|
/* Add the WebSocket service with initializing.
|
||||||
|
httpsv.AddWebSocketService<Chat> (
|
||||||
"/Chat",
|
"/Chat",
|
||||||
() => new Chat ("Anon#") {
|
() => new Chat ("Anon#") {
|
||||||
Protocol = "chat",
|
Protocol = "chat",
|
||||||
// Checking Origin header
|
// To validate the Origin header.
|
||||||
OriginValidator = value => {
|
OriginValidator = value => {
|
||||||
Uri origin;
|
Uri origin;
|
||||||
return !value.IsNullOrEmpty () &&
|
return !value.IsNullOrEmpty () &&
|
||||||
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
|
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
|
||||||
origin.Host == "localhost";
|
origin.Host == "localhost";
|
||||||
},
|
},
|
||||||
// Checking Cookies
|
// To validate the Cookies.
|
||||||
CookiesValidator = (req, res) => {
|
CookiesValidator = (req, res) => {
|
||||||
foreach (Cookie cookie in req) {
|
foreach (Cookie cookie in req) {
|
||||||
cookie.Expired = true;
|
cookie.Expired = true;
|
||||||
@ -73,40 +100,17 @@ namespace Example3
|
|||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
|
||||||
_httpsv.Start ();
|
httpsv.Start ();
|
||||||
if (_httpsv.IsListening) {
|
if (httpsv.IsListening) {
|
||||||
Console.WriteLine (
|
Console.WriteLine ("Listening on port {0}, providing WebSocket services:", httpsv.Port);
|
||||||
"An HTTP server listening on port: {0}, providing WebSocket services:", _httpsv.Port);
|
foreach (var path in httpsv.WebSocketServices.Paths)
|
||||||
|
|
||||||
foreach (var path in _httpsv.WebSocketServices.Paths)
|
|
||||||
Console.WriteLine ("- {0}", path);
|
Console.WriteLine ("- {0}", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine ("\nPress Enter key to stop the server...");
|
Console.WriteLine ("\nPress Enter key to stop the server...");
|
||||||
Console.ReadLine ();
|
Console.ReadLine ();
|
||||||
|
|
||||||
_httpsv.Stop ();
|
httpsv.Stop ();
|
||||||
}
|
|
||||||
|
|
||||||
private static byte [] getContent (string path)
|
|
||||||
{
|
|
||||||
if (path == "/")
|
|
||||||
path += "index.html";
|
|
||||||
|
|
||||||
return _httpsv.GetFile (path);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void onGet (HttpRequestEventArgs e)
|
|
||||||
{
|
|
||||||
var req = e.Request;
|
|
||||||
var res = e.Response;
|
|
||||||
var content = getContent (req.RawUrl);
|
|
||||||
if (content != null) {
|
|
||||||
res.WriteContent (content);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.StatusCode = (int) HttpStatusCode.NotFound;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,68 +1,68 @@
|
|||||||
/**
|
/*
|
||||||
* echotest.js
|
* echotest.js
|
||||||
* Derived from Echo Test of WebSocket.org (http://www.websocket.org/echo.html)
|
*
|
||||||
|
* Derived from Echo Test of WebSocket.org (http://www.websocket.org/echo.html).
|
||||||
*
|
*
|
||||||
* Copyright (c) 2012 Kaazing Corporation.
|
* Copyright (c) 2012 Kaazing Corporation.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var wsUri = "ws://localhost:4649/Echo";
|
var url = "ws://localhost:4649/Echo";
|
||||||
//var wsUri = "wss://localhost:4649/Echo";
|
//var url = "wss://localhost:4649/Echo";
|
||||||
var output;
|
var output;
|
||||||
|
|
||||||
function init(){
|
function init () {
|
||||||
output = document.getElementById("output");
|
output = document.getElementById ("output");
|
||||||
testWebSocket();
|
doWebSocket ();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testWebSocket(){
|
function doWebSocket () {
|
||||||
websocket = new WebSocket(wsUri);
|
websocket = new WebSocket (url);
|
||||||
|
|
||||||
websocket.onopen = function(evt){
|
websocket.onopen = function (evt) {
|
||||||
onOpen(evt)
|
onOpen (evt)
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onclose = function(evt){
|
websocket.onclose = function (evt) {
|
||||||
onClose(evt)
|
onClose (evt)
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onmessage = function(evt){
|
websocket.onmessage = function (evt) {
|
||||||
onMessage(evt)
|
onMessage (evt)
|
||||||
};
|
};
|
||||||
|
|
||||||
websocket.onerror = function(evt){
|
websocket.onerror = function (evt) {
|
||||||
onError(evt)
|
onError (evt)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function onOpen(evt){
|
function onOpen (evt) {
|
||||||
writeToScreen("CONNECTED");
|
writeToScreen ("CONNECTED");
|
||||||
doSend("WebSocket rocks");
|
send ("WebSocket rocks");
|
||||||
}
|
}
|
||||||
|
|
||||||
function onClose(evt){
|
function onClose (evt) {
|
||||||
writeToScreen("DISCONNECTED");
|
writeToScreen ("DISCONNECTED");
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(evt){
|
function onMessage (evt) {
|
||||||
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
|
writeToScreen ('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
|
||||||
websocket.close();
|
websocket.close ();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(evt){
|
function onError (evt) {
|
||||||
writeToScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
|
writeToScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSend(message){
|
function send (message) {
|
||||||
writeToScreen("SENT: " + message);
|
writeToScreen ("SENT: " + message);
|
||||||
websocket.send(message);
|
websocket.send (message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeToScreen(message){
|
function writeToScreen (message) {
|
||||||
var pre = document.createElement("p");
|
var pre = document.createElement ("p");
|
||||||
pre.style.wordWrap = "break-word";
|
pre.style.wordWrap = "break-word";
|
||||||
pre.innerHTML = message;
|
pre.innerHTML = message;
|
||||||
output.appendChild(pre);
|
output.appendChild (pre);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("load", init, false);
|
window.addEventListener ("load", init, false);
|
Loading…
Reference in New Issue
Block a user