コード例 #1
0
ファイル: WebSocket.cs プロジェクト: hedger/LunarServer
        /// Call this automatically from server side each keepAliveInterval period
        /// NOTE: ping payload must be 125 bytes or less
        public void SendPing()
        {
            _pingCounter++;
            _pingPayload = BitConverter.GetBytes(_pingCounter);

            var payload = new ArraySegment <byte>(_pingPayload);

            if (payload.Count > MAX_PING_PONG_PAYLOAD_LEN)
            {
                throw new InvalidOperationException($"Cannot send Ping: Max ping message size {MAX_PING_PONG_PAYLOAD_LEN} exceeded: {payload.Count}");
            }

            if (_state == WebSocketState.Open)
            {
                using (MemoryStream stream = _recycledStreamFactory())
                {
                    NeedsPing = false;
                    WebSocketFrameWriter.Write(WebSocketOpCode.Ping, payload, stream, true, _isClient);
                    _logger(LogLevel.Debug, $"websocket.Ping: {payload.Count}");
                    WriteStreamToNetwork(stream);
                }
            }
        }