本文整理汇总了C#中WebSocketState类的典型用法代码示例。如果您正苦于以下问题:C# WebSocketState类的具体用法?C# WebSocketState怎么用?C# WebSocketState使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebSocketState类属于命名空间,在下文中一共展示了WebSocketState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: tmrConnectionChecker_Tick
private void tmrConnectionChecker_Tick(object sender, EventArgs e)
{
if (firstTime)
{
firstTime = false;
try
{
Main.connection.connect(txtIP.Text, Int32.Parse(txtPort.Text));
}
catch (Exception)
{
}
}
WebSocketState st = Main.connection.state;
if (st != lastState)
{
if (st == WebSocketState.Closed)
{
btnCnnct.Enabled = true;
btnCnnct.Text = "Connect";
}
else
{
btnCnnct.Enabled = btnOK.Enabled = false;
btnCnnct.Text = Enum.GetName(typeof(WebSocketState), st);
}
btnOK.Enabled = (st == WebSocketState.Open);
lastState = st;
}
}
示例2: OwinWebSocketAdapter
public OwinWebSocketAdapter(IDictionary<string, object> websocketContext, string subProtocol)
{
_websocketContext = websocketContext;
_sendAsync = (WebSocketSendAsync)websocketContext[OwinConstants.WebSocket.SendAsync];
_receiveAsync = (WebSocketReceiveAsync)websocketContext[OwinConstants.WebSocket.ReceiveAsync];
_closeAsync = (WebSocketCloseAsync)websocketContext[OwinConstants.WebSocket.CloseAsync];
_state = WebSocketState.Open;
_subProtocol = subProtocol;
}
示例3: Dispose
public override void Dispose()
{
if (_state >= WebSocketState.Closed) // or Aborted
{
return;
}
_state = WebSocketState.Closed;
_session.WebSocketDispose();
}
示例4: CloseNoopsIfInTerminalState
public async Task CloseNoopsIfInTerminalState(WebSocketState state)
{
var webSocket = new Mock<WebSocket>();
var webSocketHandler = new Mock<WebSocketHandler>(64 * 1024, new Mock<ILogger>().Object) {CallBase = true};
webSocket.Setup(m => m.State).Returns(state);
webSocketHandler.Object.WebSocket = webSocket.Object;
await webSocketHandler.Object.CloseAsync();
webSocket.Verify(m => m.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None), Times.Never());
}
示例5: CommonWebSocket
public CommonWebSocket(Stream stream, string subProtocol, TimeSpan keepAliveInterval, int receiveBufferSize, bool maskOutput, bool useZeroMask, bool unmaskInput)
{
_stream = stream;
_subProtocl = subProtocol;
_state = WebSocketState.Open;
_receiveBuffer = new byte[receiveBufferSize];
_maskOutput = maskOutput;
_useZeroMask = useZeroMask;
_unmaskInput = unmaskInput;
_writeLock = new SemaphoreSlim(1);
if (keepAliveInterval != Timeout.InfiniteTimeSpan)
{
_keepAliveTimer = new Timer(SendKeepAlive, this, keepAliveInterval, keepAliveInterval);
}
}
示例6: StreamWebSocket
public StreamWebSocket (Stream rstream, Stream wstream, Socket socket, string subProtocol, bool maskSend, ArraySegment<byte> preloaded)
{
this.rstream = rstream;
this.wstream = wstream;
// Necessary when both of rstream and wstream doesn't close socket.
this.socket = socket;
this.subProtocol = subProtocol;
this.maskSend = maskSend;
if (maskSend) {
random = new Random ();
}
this.preloaded = preloaded;
state = WebSocketState.Open;
headerBuffer = new byte[HeaderMaxLength];
}
示例7: CommonWebSocket
protected CommonWebSocket(string subProtocol, TimeSpan keepAliveInterval, int receiveBufferSize, bool maskOutput, bool useZeroMask, bool unmaskInput)
{
_connection = new TcpClient
{
NoDelay = true
};
_state = WebSocketState.None;
_subProtocol = subProtocol;
_keepAliveInterval = keepAliveInterval;
_receiveBuffer = new byte[receiveBufferSize];
_maskOutput = maskOutput;
_useZeroMask = useZeroMask;
_unmaskInput = unmaskInput;
_writeLock = new SemaphoreSlim(1);
}
示例8: CloseOutputAsync
public async override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
{
ThrowIfDisposed();
ThrowIfOutputClosed();
var message = new Message(closeStatus, statusDescription);
await _sendBuffer.SendAsync(message, cancellationToken);
if (State == WebSocketState.Open)
{
_state = WebSocketState.CloseSent;
}
else if (State == WebSocketState.CloseReceived)
{
_state = WebSocketState.Closed;
Close();
}
}
示例9: ThrowOnInvalidState
protected static void ThrowOnInvalidState(WebSocketState state, params WebSocketState[] validStates)
{
string validStatesText = string.Empty;
if (validStates != null && validStates.Length > 0)
{
foreach (WebSocketState currentState in validStates)
{
if (state == currentState)
{
return;
}
}
validStatesText = string.Join(", ", validStates);
}
throw new WebSocketException(SR.Format(SR.net_WebSockets_InvalidState, state, validStatesText));
}
示例10: Cleanup
public void Cleanup()
{
ResponseHeaders.Clear();
Stream = null;
_state = WebSocketState.Closed;
}
示例11: Open
public void Open( string url )
{
if (IsOpen)
{
Close();
Helper.Log("FIXME: Wait for the connection to close");
}
_url = url;
try
{
_state = WebSocketState.Connecting;
makeHandshake( url );
}
catch (Exception err)
{
Helper.Log( "ERROR: " + err );
_state = WebSocketState.Closed;
throw;
}
}
示例12: ProcessFrame
/// <summary>
/// Processes a websocket frame and triggers consumer events
/// </summary>
/// <param name="psocketState">We need to modify the websocket state here depending on the frame</param>
private void ProcessFrame(WebSocketState psocketState)
{
if (psocketState.Header.IsMasked)
{
byte[] unmask = psocketState.ReceivedBytes.ToArray();
WebSocketReader.Mask(psocketState.Header.Mask, unmask);
psocketState.ReceivedBytes = new List<byte>(unmask);
}
if (psocketState.Header.Opcode != WebSocketReader.OpCode.Continue && _initialMsgTimeout > 0)
{
_receiveDone.Set();
_initialMsgTimeout = 0;
}
switch (psocketState.Header.Opcode)
{
case WebSocketReader.OpCode.Ping:
PingDelegate pingD = OnPing;
if (pingD != null)
{
pingD(this, new PingEventArgs());
}
WebSocketFrame pongFrame = new WebSocketFrame(){Header = WebsocketFrameHeader.HeaderDefault(),WebSocketPayload = new byte[0]};
pongFrame.Header.Opcode = WebSocketReader.OpCode.Pong;
pongFrame.Header.IsEnd = true;
SendSocket(pongFrame.ToBytes());
break;
case WebSocketReader.OpCode.Pong:
PongDelegate pongD = OnPong;
if (pongD != null)
{
pongD(this, new PongEventArgs(){PingResponseMS = Util.EnvironmentTickCountSubtract(Util.EnvironmentTickCount(),_pingtime)});
}
break;
case WebSocketReader.OpCode.Binary:
if (!psocketState.Header.IsEnd) // Not done, so we need to store this and wait for the end frame.
{
psocketState.ContinuationFrame = new WebSocketFrame
{
Header = psocketState.Header,
WebSocketPayload =
psocketState.ReceivedBytes.ToArray()
};
}
else
{
// Send Done Event!
DataDelegate dataD = OnData;
if (dataD != null)
{
dataD(this,new WebsocketDataEventArgs(){Data = psocketState.ReceivedBytes.ToArray()});
}
}
break;
case WebSocketReader.OpCode.Text:
if (!psocketState.Header.IsEnd) // Not done, so we need to store this and wait for the end frame.
{
psocketState.ContinuationFrame = new WebSocketFrame
{
Header = psocketState.Header,
WebSocketPayload =
psocketState.ReceivedBytes.ToArray()
};
}
else
{
TextDelegate textD = OnText;
if (textD != null)
{
textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(psocketState.ReceivedBytes.ToArray()) });
}
// Send Done Event!
}
break;
case WebSocketReader.OpCode.Continue: // Continuation. Multiple frames worth of data for one message. Only valid when not using Control Opcodes
//Console.WriteLine("currhead " + psocketState.Header.IsEnd);
//Console.WriteLine("Continuation! " + psocketState.ContinuationFrame.Header.IsEnd);
byte[] combineddata = new byte[psocketState.ReceivedBytes.Count+psocketState.ContinuationFrame.WebSocketPayload.Length];
byte[] newdata = psocketState.ReceivedBytes.ToArray();
Buffer.BlockCopy(psocketState.ContinuationFrame.WebSocketPayload, 0, combineddata, 0, psocketState.ContinuationFrame.WebSocketPayload.Length);
Buffer.BlockCopy(newdata, 0, combineddata,
psocketState.ContinuationFrame.WebSocketPayload.Length, newdata.Length);
psocketState.ContinuationFrame.WebSocketPayload = combineddata;
psocketState.Header.PayloadLen = (ulong)combineddata.Length;
if (psocketState.Header.IsEnd)
{
if (psocketState.ContinuationFrame.Header.Opcode == WebSocketReader.OpCode.Text)
{
// Send Done event
TextDelegate textD = OnText;
if (textD != null)
{
textD(this, new WebsocketTextEventArgs() { Data = Encoding.UTF8.GetString(combineddata) });
}
//.........这里部分代码省略.........
示例13: UpdateState
public void UpdateState(WebSocketState value)
{
if ((_state != WebSocketState.Closed) && (_state != WebSocketState.Aborted))
{
_state = value;
}
}
示例14: InterlockedCheckAndUpdateState
public void InterlockedCheckAndUpdateState(
WebSocketState newState,
params WebSocketState[] validStates)
{
lock (_lock)
{
CheckValidState(validStates);
UpdateState(newState);
}
}
示例15: Close
// As server
internal void Close(HttpStatusCode code)
{
_readyState = WebSocketState.CLOSING;
send (createHandshakeResponse (code));
closeServerResources ();
_readyState = WebSocketState.CLOSED;
}