Browse Source

[Modify] Polish it

pull/148/merge
sta 10 years ago
parent
commit
83aa8c9460
  1. 4
      Example3/Chat.cs
  2. 3
      Example3/Echo.cs
  3. 22
      Example3/Program.cs
  4. 32
      Example3/Public/Js/echotest.js

4
Example3/Chat.cs

@ -24,9 +24,7 @@ namespace Example3
private string getName () private string getName ()
{ {
var name = Context.QueryString["name"]; var name = Context.QueryString["name"];
return !name.IsNullOrEmpty ()
? name
: (_prefix + getNumber ());
return !name.IsNullOrEmpty () ? name : _prefix + getNumber ();
} }
private static int getNumber () private static int getNumber ()

3
Example3/Echo.cs

@ -9,8 +9,7 @@ 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 () ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data;
Send (msg);
Send (!name.IsNullOrEmpty () ? String.Format ("\"{0}\" to {1}", e.Data, name) : e.Data);
} }
} }
} }

22
Example3/Program.cs

@ -12,11 +12,11 @@ namespace Example3
{ {
public static void Main (string[] args) public static void Main (string[] args)
{ {
/* 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, or the https scheme HTTP URL.
*/
// 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, or the https scheme HTTP URL.
var httpsv = new HttpServer (4649); var httpsv = new HttpServer (4649);
//var httpsv = new HttpServer (5963, true); //var httpsv = new HttpServer (5963, true);
//var httpsv = new HttpServer (System.Net.IPAddress.Parse ("127.0.0.1"), 4649); //var httpsv = new HttpServer (System.Net.IPAddress.Parse ("127.0.0.1"), 4649);
@ -49,10 +49,10 @@ namespace Example3
}; };
*/ */
// To set the document root path.
// Set the document root path.
httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"]; httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"];
// To set the HTTP GET method event.
// Set the HTTP GET request event.
httpsv.OnGet += (sender, e) => { httpsv.OnGet += (sender, e) => {
var req = e.Request; var req = e.Request;
var res = e.Response; var res = e.Response;
@ -72,6 +72,11 @@ namespace Example3
res.ContentEncoding = Encoding.UTF8; res.ContentEncoding = Encoding.UTF8;
} }
if (path.EndsWith (".js")) {
res.ContentType = "application/javascript";
res.ContentEncoding = Encoding.UTF8;
}
res.WriteContent (content); res.WriteContent (content);
}; };
@ -89,8 +94,9 @@ namespace Example3
httpsv.AddWebSocketService<Chat> ( httpsv.AddWebSocketService<Chat> (
"/Chat", "/Chat",
() => new Chat ("Anon#") { () => new Chat ("Anon#") {
// To send the Sec-WebSocket-Protocol header that has a subprotocol name.
Protocol = "chat", Protocol = "chat",
// To emit a WebSocket.OnMessage event when receives a Ping.
// To emit a WebSocket.OnMessage event when receives a ping.
EmitOnPing = true, EmitOnPing = true,
// To ignore the Sec-WebSocket-Extensions header. // To ignore the Sec-WebSocket-Extensions header.
IgnoreExtensions = true, IgnoreExtensions = true,

32
Example3/Public/Js/echotest.js

@ -18,39 +18,39 @@ function init () {
function doWebSocket () { function doWebSocket () {
websocket = new WebSocket (url); websocket = new WebSocket (url);
websocket.onopen = function (evt) {
onOpen (evt)
websocket.onopen = function (e) {
onOpen (e);
}; };
websocket.onclose = function (evt) {
onClose (evt)
websocket.onmessage = function (e) {
onMessage (e);
}; };
websocket.onmessage = function (evt) {
onMessage (evt)
websocket.onerror = function (e) {
onError (e);
}; };
websocket.onerror = function (evt) {
onError (evt)
websocket.onclose = function (e) {
onClose (e);
}; };
} }
function onOpen (evt) {
function onOpen (event) {
writeToScreen ("CONNECTED"); writeToScreen ("CONNECTED");
send ("WebSocket rocks"); send ("WebSocket rocks");
} }
function onClose (evt) {
writeToScreen ("DISCONNECTED");
function onMessage (event) {
writeToScreen ('<span style="color: blue;">RESPONSE: ' + event.data + '</span>');
websocket.close ();
} }
function onMessage (evt) {
writeToScreen ('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
websocket.close ();
function onError (event) {
writeToScreen ('<span style="color: red;">ERROR: ' + event.data + '</span>');
} }
function onError (evt) {
writeToScreen('<span style="color: red;">ERROR: ' + evt.data + '</span>');
function onClose (event) {
writeToScreen ("DISCONNECTED");
} }
function send (message) { function send (message) {

Loading…
Cancel
Save