Ejemplo n.º 1
0
        public static StreamMessageResponse ofMessage(Message msg)
        {
            StreamMessageResponse r = new StreamMessageResponse();

            r.Ok      = (true);
            r.Message = (msg);
            return(r);
        }
Ejemplo n.º 2
0
        public static StreamMessageResponse ofUserSession(UserSession us)
        {
            StreamMessageResponse r = new StreamMessageResponse();

            r.Ok          = (true);
            r.UserSession = (us);
            return(r);
        }
        public async Task HandleMessageAsync(HttpContext context, WebSocket webSocket, string message)
        {
            StreamMessage strmMsg = JsonUtil.FromString <StreamMessage>(message);

            //await webSocket.RespondText("hi : " + message);

            try
            {
                switch (strmMsg.StreamMessageType)
                {
                case StreamMessageType.Singin:

                    String      token = strmMsg.TokenId;
                    UserSession us    = await userService.GetSessionAsync(token);

                    if (us != null)
                    {
                        logger.LogInformation("registering message listener for " + us);

                        ISubscriber sub = messageService.Subscribe(us.UserId, new WSMessageConsumer(webSocket));
                        this.userSession = us;
                        this.subscriber  = sub;

                        await webSocket.RespondText(JsonUtil.ToString(StreamMessageResponse.ofOk()));
                    }
                    else
                    {
                        logger.LogWarning("unable to find a valid session with id " + token);
                        await webSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "badtoken", CancellationToken.None);
                    }

                    break;

                case StreamMessageType.Ping:
                    logger.LogInformation("Get PING");
                    await webSocket.RespondText(JsonUtil.ToString(StreamMessageResponse.ofOk()));

                    break;

                case StreamMessageType.Info:
                    logger.LogInformation("Get Info");
                    if (this.userSession != null)
                    {
                        await webSocket.RespondText(JsonUtil.ToString(StreamMessageResponse.ofUserSession(this.userSession)));
                    }
                    else
                    {
                        await webSocket.RespondText(JsonUtil.ToString(StreamMessageResponse.ofOk()));
                    }
                    break;

                case StreamMessageType.Message:
                    break;
                }
            }
            catch (Exception ex)
            {
                await webSocket.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "exception", CancellationToken.None);

                logger.LogError(ex.ToString());
            }
        }
 public async void Handle(Message msg)
 {
     StreamMessageResponse response = StreamMessageResponse.ofMessage(msg);
     await webSocket.RespondText(JsonUtil.ToString(response));
 }