Пример #1
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (context.WebSockets.IsWebSocketRequest)
            {
                ISubProtocol subProtocol = DetectSubProtocol(context);

                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(subProtocol?.Name);

                var buffer = new byte[options.ReceiveBufferSize];

                WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

                subProtocol = subProtocol ?? options.DefaultProtocol;
                while (!result.CloseStatus.HasValue)
                {
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
                        await subProtocol.SendAsync(message, webSocket, CancellationToken.None);
                    }
                    result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
                }
                await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
            }
            else
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
            }
        }
Пример #2
0
        ISubProtocol DetectSubProtocol(HttpContext context)
        {
            ISubProtocol subProtocol = null;

            foreach (var supportedProtocol in options.SupportedProtocols)
            {
                if (context.WebSockets.WebSocketRequestedProtocols.Contains(supportedProtocol.Name))
                {
                    subProtocol = supportedProtocol;
                    break;
                }
            }

            return(subProtocol);
        }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebSocketServer"/> class.
 /// </summary>
 /// <param name="subProtocol">The sub protocol.</param>
 public WebSocketServer(ISubProtocol <WebSocketSession> subProtocol)
     : base(subProtocol)
 {
 }
Пример #4
0
 public LiveServer(ISubProtocol <LiveSession> subProtocol, IGlobalSessionsRegistry sessionsRegistry)
     : base(subProtocol)
 {
     _sessionsRegistry = sessionsRegistry;
 }
Пример #5
0
        private void MessageReceived(GameServer server, IProtocol message)
        {
            ISubProtocol pSubProtocol = message.GetSubProtocol();

            if (pSubProtocol == null ||
                (ProtocolDef)message.ProtocolId != ProtocolDef.l2e_header_def)
            {
                return;
            }
            byte[] data = message.ToBytes();
            switch ((ProtocolDef)pSubProtocol.SubProtocolID)
            {
            // ahpho
            case ProtocolDef.l2e_update_custom_info_def:
            {
                l2e_update_custom_info protocal = new l2e_update_custom_info();
                protocal.FromBytes(data);
                _customInfoStrs = protocal.GetInfoStrs();
            }
            break;

            case ProtocolDef.l2e_PlayerCount_def:
            {
                l2e_PlayerCount protocol = new l2e_PlayerCount();
                protocol.FromBytes(data);
                if (protocol.GetTotalCount() >= 0)
                {
                    UpdatePlayerCount(protocol.GetTotalCount(), server.Group);
                }

                if (protocol.GetOffliveCount() >= 0)
                {
                    UpdateOfflineCount(protocol.GetOffliveCount());
                }
            }
            break;

            //GM指令接收的消息
            case ProtocolDef.l2e_ExeGMCmd_def:
            {
                l2e_ExeGMCmd protocol = new l2e_ExeGMCmd();
                protocol.FromBytes(data);
                //l2e_ExeGMCmd protocol = (l2e_ExeGMCmd)message;
                if (protocol.ReturnCode == 1)
                {
                    UpdateGMResultCache(protocol.nSessionID, protocol.szResult);
                }
                else
                {
                    UpdateGMResultCache(protocol.nSessionID, "GM指令操作失败");
                }
            }
            break;

            case ProtocolDef.l2e_GetBasicInfo_def:
            {
                l2e_GetBasicInfo protocol = new l2e_GetBasicInfo();
                protocol.FromBytes(data);
            }
            break;

            case ProtocolDef.l2e_Who_def:
            {
                l2e_Who protocol = new l2e_Who();
                protocol.FromBytes(data);
                UpdatePlayerWho(protocol.PlayerList);
            }
            break;

            case ProtocolDef.l2e_GetGlobalVariable_def:
            {
                l2e_GetGlobalVariable protocol = new l2e_GetGlobalVariable();
                protocol.FromBytes(data);
                server.GameSetting.UpdateGlobalVariable(protocol.VariableIndex, protocol.VariableValue);
            }
            break;

            case ProtocolDef.l2e_GetGameStartTime_def:
            {
                l2e_GetGameStartTime protocol = new l2e_GetGameStartTime();
                protocol.FromBytes(data);
                int seconds = int.Parse(protocol.GameStartTime);
                _gameRunningRefreshTime = DateTime.Now;
                _gameRunningTime        = new TimeSpan(0, 0, seconds);
            }
            break;

            case ProtocolDef.l2e_ReportError_def:
            {
                l2e_ReportError protocol = new l2e_ReportError();
                protocol.FromBytes(data);
                SetModuleStateCode(protocol.Module, protocol.ErrorCode);
            }
            break;

            case ProtocolDef.l2e_info_def:
            {
                l2e_info protocol = new l2e_info();
                protocol.FromBytes(data);
                Hashtable infoPackage = Util.ConvertKeyValuePairToHashtable(protocol.Info);
                if (infoPackage != null)
                {
                    GameInfoReceived(infoPackage);
                }
            }
            break;

            case ProtocolDef.l2e_info_large_def:
            {
                l2e_info_large protocol = new l2e_info_large();
                protocol.FromBytes(data);
                Hashtable infoPackage = Util.ConvertKeyValuePairToHashtable(protocol.InfoLarge);
                if (infoPackage != null)
                {
                    GameInfoReceived(infoPackage);
                }
            }
            break;
            }
        }