本文整理汇总了C#中IWebSocketSession.GetAvailableSubProtocol方法的典型用法代码示例。如果您正苦于以下问题:C# IWebSocketSession.GetAvailableSubProtocol方法的具体用法?C# IWebSocketSession.GetAvailableSubProtocol怎么用?C# IWebSocketSession.GetAvailableSubProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWebSocketSession
的用法示例。
在下文中一共展示了IWebSocketSession.GetAvailableSubProtocol方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}