本文整理汇总了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;
}
示例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;
}
示例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));
}
}
示例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);
}
示例5: SendCloseHandshake
public override void SendCloseHandshake(IWebSocketSession session, int statusCode, string closeReason)
{
session.SocketSession.SendResponse(WebSocketConstant.ClosingHandshake, 0, WebSocketConstant.ClosingHandshake.Length);
}
示例6: Add
internal string Add(IWebSocketSession session)
{
lock (_sync) {
if (_state != ServerState.Start)
return null;
var id = createID ();
_sessions.Add (id, session);
return id;
}
}
示例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;
}
示例8: SendData
public override void SendData(IWebSocketSession session, byte[] data, int offset, int length)
{
SendPackage(session, OpCode.Binary, data, offset, length);
}
示例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);
}
}
示例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;
}
示例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));
}
}
示例12: TrySendMessage
public override bool TrySendMessage(IWebSocketSession session, string message)
{
var packageData = GetPackageData(message);
return session.TrySendRawData(packageData.Array, packageData.Offset, packageData.Count);
}
示例13: SendPing
public override void SendPing(IWebSocketSession session, byte[] ping)
{
}
示例14: TrySendData
public override bool TrySendData(IWebSocketSession session, byte[] data, int offset, int length)
{
throw new NotImplementedException();
}
示例15: SendMessage
public override void SendMessage(IWebSocketSession session, string message)
{
var packageData = GetPackageData(message);
session.SendRawData(packageData.Array, packageData.Offset, packageData.Count);
}