@@ -227,27 +227,46 @@ func (exc *StackExchange) OnConnect(c *neffos.Conn) error {
227
227
func (exc * StackExchange ) Publish (msg neffos.Message ) bool {
228
228
// channel := exc.getMessageChannel(c.ID(), msg)
229
229
channel := exc .getChannel (msg .Namespace , msg .Room , msg .To )
230
- b := msg .Serialize ()
231
230
// neffos.Debugf("[%s] publish to channel [%s] the data [%s]\n", msg.FromExplicit, channel, string(b))
232
231
232
+ err := exc .publish (channel , msg .Serialize ())
233
+ return err == nil
234
+ }
235
+
236
+ func (exc * StackExchange ) publish (channel string , b []byte ) error {
233
237
cmd := radix .FlatCmd (nil , "PUBLISH" , channel , b )
234
- err := exc .pool .Do (cmd )
238
+ return exc .pool .Do (cmd )
239
+ }
240
+
241
+ // Ask implements the server Ask feature for redis. It blocks until response.
242
+ func (exc * StackExchange ) Ask (ctx context.Context , msg neffos.Message , token string ) (response neffos.Message , err error ) {
243
+ sub := radix .PersistentPubSub ("" , "" , exc .connFunc )
244
+ msgCh := make (chan radix.PubSubMessage )
245
+ err = sub .Subscribe (msgCh , token )
235
246
if err != nil {
236
- return false
247
+ return
237
248
}
249
+ defer sub .Close ()
238
250
239
- return true
240
- }
251
+ if ! exc .Publish (msg ) {
252
+ return response , neffos .ErrWrite
253
+ }
254
+
255
+ select {
256
+ case <- ctx .Done ():
257
+ err = ctx .Err ()
258
+ case redisMsg := <- msgCh :
259
+ response = neffos .DeserializeMessage (nil , redisMsg .Message , false , false )
260
+ err = response .Err
261
+ }
241
262
242
- // Ask TODO.
243
- // Ask will implement the server Ask feature for redis. It will block until response.
244
- func (exc * StackExchange ) Ask (ctx context.Context , msg neffos.Message , token string ) (neffos.Message , error ) {
245
- panic ("Not Implemented Yet" ) // check tomorrow... I am too tired now.
263
+ return
246
264
}
247
265
248
- // NotifyAsk TODO .
266
+ // NotifyAsk notifies and unblocks a "msg" subscriber, called on a server connection's read when expects a result .
249
267
func (exc * StackExchange ) NotifyAsk (msg neffos.Message , token string ) error {
250
- panic ("Not Implemented Yet" )
268
+ msg .ClearWait ()
269
+ return exc .publish (token , msg .Serialize ())
251
270
}
252
271
253
272
// Subscribe subscribes to a specific namespace,
0 commit comments