当前位置: 首页>>代码示例>>C#>>正文


C# IWebSocketSession类代码示例

本文整理汇总了C#中IWebSocketSession的典型用法代码示例。如果您正苦于以下问题:C# IWebSocketSession类的具体用法?C# IWebSocketSession怎么用?C# IWebSocketSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


IWebSocketSession类属于命名空间,在下文中一共展示了IWebSocketSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Handshake

        public override bool Handshake(IWebSocketSession session, WebSocketReceiveFilterBase previousFilter, out IReceiveFilter<IWebSocketFragment> dataFrameReader)
        {
            if (!VersionTag.Equals(session.SecWebSocketVersion) && NextProcessor != null)
            {
                return NextProcessor.Handshake(session, previousFilter, out dataFrameReader);
            }

            dataFrameReader = null;

            session.ProtocolProcessor = this;

            if (!session.AppServer.ValidateHandshake(session, session.Items.GetValue<string>(OriginKey, string.Empty)))
                return false;

            var secWebSocketKey = session.Items.GetValue<string>(WebSocketConstant.SecWebSocketKey, string.Empty);

            if (string.IsNullOrEmpty(secWebSocketKey))
            {
                return false;
            }

            var responseBuilder = new StringBuilder();

            string secKeyAccept = string.Empty;

            try
            {
                secKeyAccept = Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(secWebSocketKey + m_Magic)));
            }
            catch (Exception)
            {
                return false;
            }

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine10);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);
            responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseAcceptLine, secKeyAccept);

            var subProtocol = session.GetAvailableSubProtocol(session.Items.GetValue<string>(WebSocketConstant.SecWebSocketProtocol, string.Empty));

            if (!string.IsNullOrEmpty(subProtocol))
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseProtocolLine, subProtocol);

            responseBuilder.AppendWithCrCf();
            byte[] data = Encoding.UTF8.GetBytes(responseBuilder.ToString());
            session.SendRawData(data, 0, data.Length);

            dataFrameReader = new WebSocketDataFrameReceiveFilter();

            return true;
        }
开发者ID:baweiji,项目名称:SuperWebSocket,代码行数:52,代码来源:DraftHybi10Processor.cs

示例2: Handshake

        public override bool Handshake(IWebSocketSession session, WebSocketReceiveFilterBase previousFilter, out IReceiveFilter<IWebSocketFragment> dataFrameReader)
        {
            var secKey1 = session.Items.GetValue<string>(WebSocketConstant.SecWebSocketKey1, string.Empty);
            var secKey2 = session.Items.GetValue<string>(WebSocketConstant.SecWebSocketKey2, string.Empty);

            dataFrameReader = null;

            if (string.IsNullOrEmpty(secKey1) && string.IsNullOrEmpty(secKey2) && NextProcessor != null)
            {
                return NextProcessor.Handshake(session, previousFilter, out dataFrameReader);
            }

            session.ProtocolProcessor = this;

            if (!session.AppServer.ValidateHandshake(session, session.Items.GetValue<string>(WebSocketConstant.Origin, string.Empty)))
                return false;

            var secKey3 = session.Items.GetValue<byte[]>(WebSocketConstant.SecWebSocketKey3, m_ZeroKeyBytes);

            var responseBuilder = new StringBuilder();

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine00);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);

            if (!string.IsNullOrEmpty(session.Origin))
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseOriginLine, session.Origin);

            responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseLocationLine, session.UriScheme, session.Host, session.Path);

            var subProtocol = session.GetAvailableSubProtocol(session.Items.GetValue<string>(WebSocketConstant.SecWebSocketProtocol, string.Empty));

            if (!string.IsNullOrEmpty(subProtocol))
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseProtocolLine, subProtocol);

            responseBuilder.AppendWithCrCf();
            byte[] data = Encoding.UTF8.GetBytes(responseBuilder.ToString());
            session.SendRawData(data, 0, data.Length);
            //Encrypt message
            byte[] secret = GetResponseSecurityKey(secKey1, secKey2, secKey3);
            session.SendRawData(secret, 0, secret.Length);

            dataFrameReader = new WebSocketDataReceiveFilter(previousFilter);

            return true;
        }
开发者ID:neo2018,项目名称:SuperWebSocket,代码行数:46,代码来源:DraftHybi00Processor.cs

示例3: HandleWebSocketSession

        async Task HandleWebSocketSession(IWebSocketSession session)
        {
            sessions[session.Id] = session;

            try
            {
                using (session)
                {
                    BroadcastMessage(string.Format("*** session-{0} connected ({1} online)", session.Id, sessions.Count));

                    while (session.IsOpen)
                    {
                        var message = await session.ReadNextMessageAsync().ConfigureAwait(false);

                        switch (message.MessageType)
                        {
                            case WebSocketMessage.Type.Text:
                                var text = (WebSocketMessage.Text)message;
                                BroadcastMessage(string.Format("<session-{0}> {1}", session.Id, text.Payload));
                                break;

                            case WebSocketMessage.Type.Close:
                                session.CloseSession();
                                break;

                            case WebSocketMessage.Type.Ping:
                                session.SendPong();
                                break;

                            case WebSocketMessage.Type.Pong:
                                break;
                        }
                    }
                }
            }
            finally
            {
                sessions.TryRemove(session.Id, out session);
                BroadcastMessage(string.Format("*** session-{0} disconnected", session.Id));
            }
        }
开发者ID:joongonn,项目名称:mHttp,代码行数:41,代码来源:Program.cs

示例4: SendMessage

        public override void SendMessage(IWebSocketSession session, string message)
        {
            var maxByteCount = Encoding.UTF8.GetMaxByteCount(message.Length) + 2;
            var sendBuffer = new byte[maxByteCount];
            sendBuffer[0] = WebSocketConstant.StartByte;
            int bytesCount = Encoding.UTF8.GetBytes(message, 0, message.Length, sendBuffer, 1);
            sendBuffer[1 + bytesCount] = WebSocketConstant.EndByte;

            session.SocketSession.SendResponse(sendBuffer, 0, bytesCount + 2);
        }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:10,代码来源:DraftHybi00Processor.cs

示例5: SendCloseHandshake

 public override void SendCloseHandshake(IWebSocketSession session, int statusCode, string closeReason)
 {
     session.SocketSession.SendResponse(WebSocketConstant.ClosingHandshake, 0, WebSocketConstant.ClosingHandshake.Length);
 }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:4,代码来源:DraftHybi00Processor.cs

示例6: Add

        internal string Add(IWebSocketSession session)
        {
            lock (_sync) {
            if (_state != ServerState.Start)
              return null;

            var id = createID ();
            _sessions.Add (id, session);

            return id;
              }
        }
开发者ID:ppatel2,项目名称:websocket-sharp,代码行数:12,代码来源:WebSocketSessionManager.cs

示例7: tryGetSession

    private bool tryGetSession (string id, out IWebSocketSession session)
    {
      bool result;
      lock (_sync)
      {
        result = _sessions.TryGetValue (id, out session);
      }

      if (!result)
        _logger.Error ("A WebSocket session with the specified ID not found.\nID: " + id);

      return result;
    }
开发者ID:songotony,项目名称:RType-Client,代码行数:13,代码来源:WebSocketSessionManager.cs

示例8: SendData

 public override void SendData(IWebSocketSession session, byte[] data, int offset, int length)
 {
     SendPackage(session, OpCode.Binary, data, offset, length);
 }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:4,代码来源:DraftHybi10Processor.cs

示例9: SendCloseHandshake

        public override void SendCloseHandshake(IWebSocketSession session, int statusCode, string closeReason)
        {
            byte[] playloadData = new byte[(string.IsNullOrEmpty(closeReason) ? 0 : Encoding.UTF8.GetMaxByteCount(closeReason.Length)) + 2];

            int highByte = statusCode / 256;
            int lowByte = statusCode % 256;

            playloadData[0] = (byte)highByte;
            playloadData[1] = (byte)lowByte;

            if (!string.IsNullOrEmpty(closeReason))
            {
                int bytesCount = Encoding.UTF8.GetBytes(closeReason, 0, closeReason.Length, playloadData, 2);
                SendPackage(session, OpCode.Close, playloadData, 0, bytesCount + 2);
            }
            else
            {
                SendPackage(session, OpCode.Close, playloadData, 0, playloadData.Length);
            }
        }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:20,代码来源:DraftHybi10Processor.cs

示例10: Handshake

 public bool Handshake(IWebSocketSession session, WebSocketReceiveFilterBase previousReader, out IReceiveFilter<IWebSocketFragment> dataFrameReader)
 {
     dataFrameReader = null;
     session.SendRawData(m_SwitchResponse, 0, m_SwitchResponse.Length);
     return true;
 }
开发者ID:klbr,项目名称:SuperWebSocket,代码行数:6,代码来源:MultipleProtocolSwitchProcessor.cs

示例11: SendPackage

        private void SendPackage(IWebSocketSession session, int opCode, byte[] data, int offset, int length)
        {
            byte[] fragment;

            if (length < 126)
            {
                fragment = new byte[2 + length];
                fragment[1] = (byte)length;
            }
            else if (length < 65536)
            {
                fragment = new byte[4 + length];
                fragment[1] = (byte)126;
                fragment[2] = (byte)(length / 256);
                fragment[3] = (byte)(length % 256);
            }
            else
            {
                fragment = new byte[10 + length];
                fragment[1] = (byte)127;

                int left = length;
                int unit = 256;

                for (int i = 9; i > 1; i--)
                {
                    fragment[i] = (byte)(left % unit);
                    left = left / unit;

                    if (left == 0)
                        break;
                }
            }

            fragment[0] = (byte)(opCode | 0x80);

            if (length > 0)
            {
                Buffer.BlockCopy(data, offset, fragment, fragment.Length - length, length);
                session.EnqueueSend(new ArraySegment<byte>(fragment, 0, fragment.Length));
            }
            else
            {
                session.EnqueueSend(new ArraySegment<byte>(fragment, 0, fragment.Length));
            }
        }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:46,代码来源:DraftHybi10Processor.cs

示例12: TrySendMessage

 public override bool TrySendMessage(IWebSocketSession session, string message)
 {
     var packageData = GetPackageData(message);
     return session.TrySendRawData(packageData.Array, packageData.Offset, packageData.Count);
 }
开发者ID:neo2018,项目名称:SuperWebSocket,代码行数:5,代码来源:DraftHybi00Processor.cs

示例13: SendPing

        public override void SendPing(IWebSocketSession session, byte[] ping)
        {

        }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:4,代码来源:DraftHybi00Processor.cs

示例14: TrySendData

 public override bool TrySendData(IWebSocketSession session, byte[] data, int offset, int length)
 {
     throw new NotImplementedException();
 }
开发者ID:neo2018,项目名称:SuperWebSocket,代码行数:4,代码来源:DraftHybi00Processor.cs

示例15: SendMessage

 public override void SendMessage(IWebSocketSession session, string message)
 {
     var packageData = GetPackageData(message);
     session.SendRawData(packageData.Array, packageData.Offset, packageData.Count);
 }
开发者ID:neo2018,项目名称:SuperWebSocket,代码行数:5,代码来源:DraftHybi00Processor.cs


注:本文中的IWebSocketSession类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。