@@ -30,6 +30,7 @@ public class RedisPubSubServer : IRedisPubSubServer
30
30
/// Callback fired on each message received, handle with (channel, msg) => ...
31
31
/// </summary>
32
32
public Action < string , string > OnMessage { get ; set ; }
33
+ public Action < string , byte [ ] > OnMessageBytes { get ; set ; }
33
34
34
35
public Action < string > OnControlCommand { get ; set ; }
35
36
public Action < string > OnUnSubscribe { get ; set ; }
@@ -55,8 +56,8 @@ public class RedisPubSubServer : IRedisPubSubServer
55
56
private int autoRestart = YES ;
56
57
public bool AutoRestart
57
58
{
58
- get { return Interlocked . CompareExchange ( ref autoRestart , 0 , 0 ) == YES ; }
59
- set { Interlocked . CompareExchange ( ref autoRestart , value ? YES : NO , autoRestart ) ; }
59
+ get => Interlocked . CompareExchange ( ref autoRestart , 0 , 0 ) == YES ;
60
+ set => Interlocked . CompareExchange ( ref autoRestart , value ? YES : NO , autoRestart ) ;
60
61
}
61
62
62
63
public DateTime CurrentServerTime => new DateTime ( serverTimeAtStart . Ticks + startedAt . ElapsedTicks , DateTimeKind . Utc ) ;
@@ -231,18 +232,35 @@ private void RunLoop()
231
232
{
232
233
subscription . OnUnSubscribe = HandleUnSubscribe ;
233
234
235
+ if ( OnMessageBytes != null )
236
+ {
237
+ bool IsCtrlMessage ( byte [ ] msg )
238
+ {
239
+ if ( msg . Length < 4 )
240
+ return false ;
241
+ return msg [ 0 ] == 'C' && msg [ 1 ] == 'T' && msg [ 0 ] == 'R' && msg [ 0 ] == 'L' ;
242
+ }
243
+
244
+ ( ( RedisSubscription ) subscription ) . OnMessageBytes = ( channel , msg ) => {
245
+ if ( IsCtrlMessage ( msg ) )
246
+ return ;
247
+
248
+ OnMessageBytes ( channel , msg ) ;
249
+ } ;
250
+ }
251
+
234
252
subscription . OnMessage = ( channel , msg ) =>
235
253
{
236
254
if ( string . IsNullOrEmpty ( msg ) )
237
255
return ;
238
256
239
- var ctrlMsg = msg . SplitOnFirst ( ':' ) ;
240
- if ( ctrlMsg [ 0 ] == ControlCommand . Control )
257
+ var ctrlMsg = msg . LeftPart ( ':' ) ;
258
+ if ( ctrlMsg == ControlCommand . Control )
241
259
{
242
260
var op = Interlocked . CompareExchange ( ref doOperation , Operation . NoOp , doOperation ) ;
243
261
244
- var msgType = ctrlMsg . Length > 1
245
- ? ctrlMsg [ 1 ]
262
+ var msgType = msg . IndexOf ( ':' ) >= 0
263
+ ? msg . RightPart ( ':' )
246
264
: null ;
247
265
248
266
OnControlCommand ? . Invoke ( msgType ?? Operation . GetName ( op ) ) ;
0 commit comments