@@ -34,8 +34,13 @@ public final class WebSocket {
34
34
private var waitingForPong : Bool
35
35
private var waitingForClose : Bool
36
36
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
+ ) {
39
44
self . channel = channel
40
45
self . type = type
41
46
self . onTextCallback = { _, _ in }
@@ -45,6 +50,7 @@ public final class WebSocket {
45
50
self . waitingForPong = false
46
51
self . waitingForClose = false
47
52
self . scheduledTimeoutTask = nil
53
+ self . outboundMaxFrameSize = outboundMaxFrameSize
48
54
}
49
55
50
56
public func onText( _ callback: @escaping ( WebSocket , String ) -> ( ) ) {
@@ -88,12 +94,65 @@ public final class WebSocket {
88
94
let string = String ( text)
89
95
var buffer = channel. allocator. buffer ( capacity: text. count)
90
96
buffer. writeString ( string)
91
- self . send ( raw: buffer. readableBytesView, opcode: . text, fin: true , promise: promise)
92
97
98
+ self . send ( buffer: buffer, opcode: . text, promise: promise)
93
99
}
94
100
95
101
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)
97
156
}
98
157
99
158
public func sendPing( promise: EventLoopPromise < Void > ? = nil ) {
0 commit comments