Esempio n. 1
0
        public Boolean AddSubscriber(WsConfSubscriber subscriber)
        {
            bool result = false;

            lock (this)
            {
                if (noOfModifiers++ == 0)
                {
                    hashSetChangeDone.Reset();
                }
                if (noOfBroadcasters > 0)
                {
                    Monitor.Wait(this);
                }
            }
            try
            {
                result = subscribers.Add(subscriber);
            }
            finally
            {
                lock (this)
                {
                    if (--noOfModifiers == 0)
                    {
                        hashSetChangeDone.Set();
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
 public void RemoveSubscriber(WsConfSubscriber subscriber)
 {
     lock (this)
     {
         if (noOfModifiers++ == 0)
         {
             hashSetChangeDone.Reset();
         }
         if (noOfBroadcasters > 0)
         {
             Monitor.Wait(this);
         }
     }
     try
     {
         subscribers.Remove(subscriber);
         if (disposedSubs != null)
         {
             disposedSubs.Remove(subscriber);
         }
     }
     finally
     {
         lock (this)
         {
             if (--noOfModifiers == 0)
             {
                 hashSetChangeDone.Set();
             }
         }
     }
 }
 public void OnClose(WsConfSubscriber subsciber, WebSocketCloseStatus status)
 {
     WsConfPubSubState confState;
     if (channels.TryGetValue(channelName, out confState))
     {
         confState.RemoveSubscriber(subsciber);
     }
 }
        public async Task OnOpen(WsConfSubscriber subscriber)
        {
            WsConfPubSubState confState;
            Boolean substate = false;
            if (!channels.TryGetValue(channelName, out confState))
            {
                //add from databse 
                confState = channels.GetOrAdd(channelName, new WsConfPubSubState(channelName));

            }
            substate = confState.AddSubscriber(subscriber);
            string sendLog = "{\"Message\":\"Success Connected\"}";
            ArraySegment<byte> welcomeBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(sendLog));
            Boolean gotSignal = false;
            try
            {
                gotSignal = subscriber.autoREvent.WaitOne();
                //await subscriber.subSocket.SendAsync(welcomeBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
            }
            finally
            {
                if (gotSignal) subscriber.autoREvent.Set();
            }
        }
        private async Task HandleWebSocket(WebSocketContext wsContext)
        {
            const int maxMessageSize = 102400;
            byte[] receiveBuffer = new byte[maxMessageSize];
            WebSocket subSocket = wsContext.WebSocket;
            WsConfSubscriber subscriber = new WsConfSubscriber(subSocket);
            await OnOpen(subscriber);
            while (true)
            {
                WebSocketReceiveResult receiveResult = null;
                try
                {
                    receiveResult = await subSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
                }
                catch (WebSocketException wse)
                {
                    OnClose(subscriber, WebSocketCloseStatus.InvalidMessageType);

                    break;
                }

                if (receiveResult.MessageType == WebSocketMessageType.Close)
                {
                    OnClose(subscriber, receiveResult.CloseStatus.Value);
                    await subSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
                }
                else if (receiveResult.MessageType == WebSocketMessageType.Binary)
                {
                    OnClose(subscriber, WebSocketCloseStatus.InvalidMessageType);
                    await subSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Binary frame not allowed", CancellationToken.None);
                }
                else
                {
                    int count = receiveResult.Count;
                    while (receiveResult.EndOfMessage == false)
                    {
                        if (count >= maxMessageSize)
                        {
                            string closeMessage = string.Format("Max message size: {0} bytes.", maxMessageSize);
                            OnClose(subscriber, WebSocketCloseStatus.MessageTooBig);
                            await subSocket.CloseAsync(WebSocketCloseStatus.MessageTooBig, closeMessage, CancellationToken.None);
                            return;
                        }
                        receiveResult = await subSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer, count, maxMessageSize - count), CancellationToken.None);
                        if (receiveResult.Count == 0)
                        {

                        }
                        count += receiveResult.Count;
                    }
                    //var receivedString = Encoding.UTF8.GetString(receiveBuffer, 0, count);
                }
            }
            //WebSocket socket = context.WebSocket;
            //while (true)
            //{
            //    ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
            //    WebSocketReceiveResult result = await socket.ReceiveAsync(
            //        buffer, CancellationToken.None);
            //    if (socket.State == WebSocketState.Open)
            //    {
            //        string userMessage = Encoding.UTF8.GetString(
            //            buffer.Array, 0, result.Count);
            //        userMessage = "You sent: " + userMessage + " at " +
            //            DateTime.Now.ToLongTimeString();
            //        buffer = new ArraySegment<byte>(
            //            Encoding.UTF8.GetBytes(userMessage));
            //        await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
            //    }
            //    else
            //    {
            //        break;
            //    }
            //}
        }