Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3ff1f6992b
Fetching contributors…

Cannot retrieve contributors at this time

file 34 lines (29 sloc) 0.946 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.Web.WebSockets;

namespace AspNetChat
{
    public class WebSocketChatHandler : WebSocketHandler
    {
        private static WebSocketCollection clients = new WebSocketCollection();
        private string name;
        
        public override void OnOpen()
        {
            this.name = this.WebSocketContext.QueryString["username"];
            clients.Add(this);

            clients.Broadcast(string.Format("{0} joined.", name));
        }
        
        public override void OnMessage(string message)
        {
            clients.Broadcast(string.Format("{0}: {1}", name, message));
        }

        public override void OnClose()
        {
            clients.Remove(this);
            clients.Broadcast(string.Format("{0} left.", name));
        }
    }
}
Something went wrong with that request. Please try again.