Browse Source

Modified a few for Example3

pull/80/head
sta 11 years ago
parent
commit
74ec969758
  1. 1
      Example3/AssemblyInfo.cs
  2. 11
      Example3/Chat.cs
  3. 5
      Example3/Echo.cs
  4. 110
      Example3/Program.cs
  5. 20
      Example3/Public/Js/echotest.js

1
Example3/AssemblyInfo.cs

@ -24,4 +24,3 @@ using System.Runtime.CompilerServices;
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

11
Example3/Chat.cs

@ -7,9 +7,8 @@ namespace Example3
{
public class Chat : WebSocketBehavior
{
private static int _num = 0;
private string _name;
private static int _number = 0;
private string _prefix;
public Chat ()
@ -25,12 +24,14 @@ namespace Example3
private string getName ()
{
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 ()

5
Example3/Echo.cs

@ -9,10 +9,7 @@ namespace Example3
protected override void OnMessage (MessageEventArgs e)
{
var name = Context.QueryString["name"];
var msg = !name.IsNullOrEmpty ()
? String.Format ("'{0}' to {1}", e.Data, name)
: e.Data;
var msg = !name.IsNullOrEmpty () ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data;
Send (msg);
}
}

110
Example3/Program.cs

@ -1,6 +1,7 @@
using System;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
@ -9,26 +10,32 @@ namespace Example3
{
public class Program
{
private static HttpServer _httpsv;
public static void Main (string[] args)
{
_httpsv = new HttpServer (4649);
//_httpsv = new HttpServer (4649, true); // For Secure Connection
/* Create a new instance of the HttpServer class.
*
* 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
// Changing the logging level
_httpsv.Log.Level = LogLevel.Trace;
// To change the logging level.
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
/* For Secure Connection
/* To provide the secure connection.
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var password = ConfigurationManager.AppSettings["CertFilePassword"];
_httpsv.Certificate = new X509Certificate2 (cert, password);
httpsv.Certificate = new X509Certificate2 (cert, password);
*/
/* For HTTP Authentication (Basic/Digest)
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
_httpsv.Realm = "WebSocket Test";
_httpsv.UserCredentialsFinder = identity => {
/* To provide the HTTP Authentication (Basic/Digest).
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
httpsv.Realm = "WebSocket Test";
httpsv.UserCredentialsFinder = identity => {
var expected = "nobita";
return identity.Name == expected
? new NetworkCredential (expected, "password", "gunfighter")
@ -36,32 +43,52 @@ namespace Example3
};
*/
// Not to remove inactive clients in WebSocket services periodically
//_httpsv.KeepClean = false;
// To set the document root path.
httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"];
// To set the HTTP GET method event.
httpsv.OnGet += (sender, e) => {
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
if (path == "/")
path += "index.html";
// Setting the document root path
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
var content = httpsv.GetFile (path);
if (content == null) {
res.StatusCode = (int) HttpStatusCode.NotFound;
return;
}
if (path.EndsWith (".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
res.WriteContent (content);
};
// Setting HTTP method events
_httpsv.OnGet += (sender, e) => onGet (e);
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
// Adding WebSocket services
_httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat");
// Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
/* With initializing
_httpsv.AddWebSocketService<Chat> (
/* Add the WebSocket service with initializing.
httpsv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat ("Anon#") {
Protocol = "chat",
// Checking Origin header
// To validate the Origin header.
OriginValidator = value => {
Uri origin;
return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
// Checking Cookies
// To validate the Cookies.
CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) {
cookie.Expired = true;
@ -73,40 +100,17 @@ namespace Example3
});
*/
_httpsv.Start ();
if (_httpsv.IsListening) {
Console.WriteLine (
"An HTTP server listening on port: {0}, providing WebSocket services:", _httpsv.Port);
foreach (var path in _httpsv.WebSocketServices.Paths)
httpsv.Start ();
if (httpsv.IsListening) {
Console.WriteLine ("Listening on port {0}, providing WebSocket services:", httpsv.Port);
foreach (var path in httpsv.WebSocketServices.Paths)
Console.WriteLine ("- {0}", path);
}
Console.WriteLine ("\nPress Enter key to stop the server...");
Console.ReadLine ();
_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;
httpsv.Stop ();
}
}
}

20
Example3/Public/Js/echotest.js

@ -1,22 +1,22 @@
/**
/*
* echotest.js
* Derived from Echo Test of WebSocket.org (http://www.websocket.org/echo.html)
*
* Copyright (c) 2012 Kaazing Corporation.
* Derived from Echo Test of WebSocket.org (http://www.websocket.org/echo.html).
*
* Copyright (c) 2012 Kaazing Corporation.
*/
var wsUri = "ws://localhost:4649/Echo";
//var wsUri = "wss://localhost:4649/Echo";
var url = "ws://localhost:4649/Echo";
//var url = "wss://localhost:4649/Echo";
var output;
function init () {
output = document.getElementById ("output");
testWebSocket();
doWebSocket ();
}
function testWebSocket(){
websocket = new WebSocket(wsUri);
function doWebSocket () {
websocket = new WebSocket (url);
websocket.onopen = function (evt) {
onOpen (evt)
@ -37,7 +37,7 @@ function testWebSocket(){
function onOpen (evt) {
writeToScreen ("CONNECTED");
doSend("WebSocket rocks");
send ("WebSocket rocks");
}
function onClose (evt) {
@ -53,7 +53,7 @@ function onError(evt){
writeToScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
}
function doSend(message){
function send (message) {
writeToScreen ("SENT: " + message);
websocket.send (message);
}

Loading…
Cancel
Save