@@ -34,8 +34,13 @@ public final class WebSocket {
3434 private var waitingForPong : Bool
3535 private var waitingForClose : Bool
3636 private var scheduledTimeoutTask : Scheduled < Void > ?
37-
38- init ( channel: Channel , type: PeerType ) {
37+ private var outboundMaxFrameSize : WebSocketMaxFrameSize
38+
39+ init (
40+ channel: Channel ,
41+ type: PeerType ,
42+ outboundMaxFrameSize: WebSocketMaxFrameSize = . default
43+ ) {
3944 self . channel = channel
4045 self . type = type
4146 self . onTextCallback = { _, _ in }
@@ -45,6 +50,7 @@ public final class WebSocket {
4550 self . waitingForPong = false
4651 self . waitingForClose = false
4752 self . scheduledTimeoutTask = nil
53+ self . outboundMaxFrameSize = outboundMaxFrameSize
4854 }
4955
5056 public func onText( _ callback: @escaping ( WebSocket , String ) -> ( ) ) {
@@ -88,12 +94,65 @@ public final class WebSocket {
8894 let string = String ( text)
8995 var buffer = channel. allocator. buffer ( capacity: text. count)
9096 buffer. writeString ( string)
91- self . send ( raw: buffer. readableBytesView, opcode: . text, fin: true , promise: promise)
9297
98+ self . send ( buffer: buffer, opcode: . text, promise: promise)
9399 }
94100
95101 public func send( _ binary: [ UInt8 ] , promise: EventLoopPromise < Void > ? = nil ) {
96- self . send ( raw: binary, opcode: . binary, fin: true , promise: promise)
102+ var buffer = channel. allocator. buffer ( capacity: binary. count)
103+ buffer. writeBytes ( binary)
104+ self . send ( buffer: buffer, opcode: . binary, promise: promise)
105+ }
106+
107+ public func send(
108+ buffer: NIO . ByteBuffer ,
109+ opcode: WebSocketOpcode ,
110+ promise: EventLoopPromise < Void > ? = nil
111+ ) {
112+ guard buffer. readableBytes > outboundMaxFrameSize. value else {
113+ let frame = WebSocketFrame (
114+ fin: true ,
115+ opcode: opcode,
116+ maskKey: self . makeMaskKey ( ) ,
117+ data: buffer
118+ )
119+ self . channel. writeAndFlush ( frame, promise: promise)
120+ return
121+ }
122+
123+ var buffer = buffer
124+
125+ var framesToSend : [ WebSocketFrame ] = [ ]
126+
127+ while let frameBuffer = buffer. readSlice ( length: outboundMaxFrameSize. value) {
128+ let frame = WebSocketFrame (
129+ fin: buffer. readableBytes == 0 ,
130+ opcode: opcode,
131+ maskKey: self . makeMaskKey ( ) ,
132+ data: frameBuffer
133+ )
134+ framesToSend. append ( frame)
135+ }
136+
137+ if buffer. readableBytes > 0 {
138+ let frame = WebSocketFrame (
139+ fin: true ,
140+ opcode: opcode,
141+ maskKey: self . makeMaskKey ( ) ,
142+ data: buffer
143+ )
144+ framesToSend. append ( frame)
145+ }
146+
147+ let startingOut : EventLoopFuture < Void > = self . channel. eventLoop. makeSucceededFuture ( Void ( ) )
148+
149+ let future : EventLoopFuture < Void > = framesToSend. reduce ( startingOut) { future, frame in
150+ return future. flatMap { _ in
151+ self . channel. writeAndFlush ( frame)
152+ }
153+ }
154+
155+ promise? . completeWith ( future)
97156 }
98157
99158 public func sendPing( promise: EventLoopPromise < Void > ? = nil ) {
0 commit comments