@@ -34,8 +34,8 @@ public final class WebSocket: Sendable {
34
34
internal let channel : Channel
35
35
private let onTextCallback : NIOLoopBoundBox < @Sendable ( WebSocket , String ) -> ( ) >
36
36
private let onBinaryCallback : NIOLoopBoundBox < @Sendable ( WebSocket , ByteBuffer ) -> ( ) >
37
- private let onPongCallback : NIOLoopBoundBox < @Sendable ( WebSocket ) -> ( ) >
38
- private let onPingCallback : NIOLoopBoundBox < @Sendable ( WebSocket ) -> ( ) >
37
+ private let onPongCallback : NIOLoopBoundBox < @Sendable ( WebSocket , ByteBuffer ) -> ( ) >
38
+ private let onPingCallback : NIOLoopBoundBox < @Sendable ( WebSocket , ByteBuffer ) -> ( ) >
39
39
private let type : PeerType
40
40
private let waitingForPong : NIOLockedValueBox < Bool >
41
41
private let waitingForClose : NIOLockedValueBox < Bool >
@@ -48,8 +48,8 @@ public final class WebSocket: Sendable {
48
48
self . type = type
49
49
self . onTextCallback = . init( { _, _ in } , eventLoop: channel. eventLoop)
50
50
self . onBinaryCallback = . init( { _, _ in } , eventLoop: channel. eventLoop)
51
- self . onPongCallback = . init( { _ in } , eventLoop: channel. eventLoop)
52
- self . onPingCallback = . init( { _ in } , eventLoop: channel. eventLoop)
51
+ self . onPongCallback = . init( { _, _ in } , eventLoop: channel. eventLoop)
52
+ self . onPingCallback = . init( { _, _ in } , eventLoop: channel. eventLoop)
53
53
self . waitingForPong = . init( false )
54
54
self . waitingForClose = . init( false )
55
55
self . scheduledTimeoutTask = . init( nil )
@@ -66,13 +66,23 @@ public final class WebSocket: Sendable {
66
66
self . onBinaryCallback. value = callback
67
67
}
68
68
69
- @ preconcurrency public func onPong( _ callback: @Sendable @escaping ( WebSocket ) -> ( ) ) {
69
+ public func onPong( _ callback: @Sendable @escaping ( WebSocket , ByteBuffer ) -> ( ) ) {
70
70
self . onPongCallback. value = callback
71
71
}
72
+
73
+ @available ( * , deprecated, message: " Please use `onPong { socket, data in /* … */ }` with the additional `data` parameter. " )
74
+ @preconcurrency public func onPong( _ callback: @Sendable @escaping ( WebSocket ) -> ( ) ) {
75
+ self . onPongCallback. value = { ws, _ in callback ( ws) }
76
+ }
72
77
73
- @ preconcurrency public func onPing( _ callback: @Sendable @escaping ( WebSocket ) -> ( ) ) {
78
+ public func onPing( _ callback: @Sendable @escaping ( WebSocket , ByteBuffer ) -> ( ) ) {
74
79
self . onPingCallback. value = callback
75
80
}
81
+
82
+ @available ( * , deprecated, message: " Please use `onPing { socket, data in /* … */ }` with the additional `data` parameter. " )
83
+ @preconcurrency public func onPing( _ callback: @Sendable @escaping ( WebSocket ) -> ( ) ) {
84
+ self . onPingCallback. value = { ws, _ in callback ( ws) }
85
+ }
76
86
77
87
/// If set, this will trigger automatic pings on the connection. If ping is not answered before
78
88
/// the next ping is sent, then the WebSocket will be presumed inactive and will be closed
@@ -112,8 +122,12 @@ public final class WebSocket: Sendable {
112
122
}
113
123
114
124
public func sendPing( promise: EventLoopPromise < Void > ? = nil ) {
125
+ sendPing ( Data ( ) , promise: promise)
126
+ }
127
+
128
+ public func sendPing( _ data: Data , promise: EventLoopPromise < Void > ? = nil ) {
115
129
self . send (
116
- raw: Data ( ) ,
130
+ raw: data ,
117
131
opcode: . ping,
118
132
fin: true ,
119
133
promise: promise
@@ -236,7 +250,7 @@ public final class WebSocket: Sendable {
236
250
if let maskingKey = maskingKey {
237
251
frameData. webSocketUnmask ( maskingKey)
238
252
}
239
- self . onPingCallback. value ( self )
253
+ self . onPingCallback. value ( self , ByteBuffer ( buffer : frameData ) )
240
254
self . send (
241
255
raw: frameData. readableBytesView,
242
256
opcode: . pong,
@@ -254,7 +268,7 @@ public final class WebSocket: Sendable {
254
268
frameData. webSocketUnmask ( maskingKey)
255
269
}
256
270
self . waitingForPong. withLockedValue { $0 = false }
257
- self . onPongCallback. value ( self )
271
+ self . onPongCallback. value ( self , ByteBuffer ( buffer : frameData ) )
258
272
} else {
259
273
self . close ( code: . protocolError, promise: nil )
260
274
}
0 commit comments