kerryjiang / SuperSocket

SuperSocket is a light weight, cross platform and extensible socket server application framework.
Apache License 2.0
3.95k stars 1.15k forks source link

2.0版本,WebSocket怎么捕获Command未匹配成功的未知消息 #604

Open 18682788952 opened 2 years ago

chucklu commented 2 years ago

Have no idea about this, you might check this file src\SuperSocket.WebSocket.Server\WebSocketPackageHandler.cs Then find the implementation of IWebSocketCommandMiddleware and ISubProtocolHandler

_websocketCommandMiddleware = serviceProvider
                .GetService<IWebSocketCommandMiddleware>() as IPackageHandler<WebSocketPackage>;

            _subProtocolHandlers = serviceProvider.GetServices<ISubProtocolHandler>().ToDictionary(h => h.Name, StringComparer.OrdinalIgnoreCase);

Also check the logic in Handle method

 public async ValueTask Handle(IAppSession session, WebSocketPackage package)
        {
            var websocketSession = session as WebSocketSession;

            if (package.OpCode == OpCode.Handshake)
            {
                websocketSession.HttpHeader = package.HttpHeader;

                // handshake failure
                if (!(await HandleHandshake(websocketSession, package)))
                {
                    websocketSession.CloseWithoutHandshake();
                    return;
                }

                websocketSession.Handshaked = true;
                await _websocketServerMiddleware.HandleSessionHandshakeCompleted(websocketSession);
                return;
            }

            if (!websocketSession.Handshaked)
            {
                // not pass handshake but receive data package now
                // impossible routine
                return;
            }

            if (package.OpCode == OpCode.Close)
            {
                if (websocketSession.CloseStatus == null)
                {
                    var closeStatus = GetCloseStatusFromPackage(package);

                    websocketSession.CloseStatus = closeStatus;

                    try
                    {
                        await websocketSession.SendAsync(package);
                    }
                    catch (InvalidOperationException)
                    {
                         // support the case the client close the connection right after it send the close handshake
                    }
                }
                else
                {
                    websocketSession.CloseWithoutHandshake();
                }

                return;
            }
            else if (package.OpCode == OpCode.Ping)
            {
                package.OpCode = OpCode.Pong;
                await websocketSession.SendAsync(package);
                return;
            }
            else if (package.OpCode == OpCode.Pong)
            {
                // don't do anything for Pong for now
                return;
            }

            var protocolHandler = websocketSession.SubProtocolHandler;

            if (protocolHandler != null)
            {
                await protocolHandler.Handle(session, package);
                return;
            }

            // application command
            var websocketCommandMiddleware = _websocketCommandMiddleware;

            if (websocketCommandMiddleware != null)
            {
                await websocketCommandMiddleware.Handle(session, package);
                return;
            }

            var packageHandleDelegate = _packageHandlerDelegate;

            if (packageHandleDelegate != null)
                await packageHandleDelegate(websocketSession, package);
        }