From 174d3ff00eb2187401547b02ea4d1073128e2e1b Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 7 Aug 2017 15:42:25 +0900 Subject: [PATCH] [Modify] Add it --- .../Server/WebSocketSessionManager.cs | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs index 7c1acc92..8252ec85 100644 --- a/websocket-sharp/Server/WebSocketSessionManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -467,6 +467,79 @@ namespace WebSocketSharp.Server broadcast (Opcode.Text, new MemoryStream (bytes), null); } + /// + /// Sends the specified of data from + /// the specified to every client in + /// the WebSocket service. + /// + /// + /// A from which to read the binary data to send. + /// + /// + /// An that specifies the number of bytes to send. + /// + /// + /// The current state of the manager is not Start. + /// + /// + /// is . + /// + /// + /// + /// cannot be read. + /// + /// + /// -or- + /// + /// + /// is less than 1. + /// + /// + /// -or- + /// + /// + /// No data could be read from . + /// + /// + public void Broadcast (Stream stream, int length) + { + if (_state != ServerState.Start) { + var msg = "The current state of the manager is not Start."; + throw new InvalidOperationException (msg); + } + + if (stream == null) + throw new ArgumentNullException ("stream"); + + if (!stream.CanRead) + throw new ArgumentException ("It cannot be read.", "stream"); + + if (length < 1) + throw new ArgumentException ("Less than 1.", "length"); + + var bytes = stream.ReadBytes (length); + + var len = bytes.Length; + if (len == 0) { + var msg = "No data could be read from it."; + throw new ArgumentException (msg, "stream"); + } + + if (len < length) { + _logger.Warn ( + String.Format ( + "Only {0} byte(s) of data could be read from the specified stream.", + len + ) + ); + } + + if (len <= WebSocket.FragmentLength) + broadcast (Opcode.Binary, bytes, null); + else + broadcast (Opcode.Binary, new MemoryStream (bytes), null); + } + /// /// Sends the specified asynchronously to /// every client in the WebSocket service.