forked from lutianzhou001/neo3fura
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from Tobeyw/dev-mindy
add bridge for ws
- Loading branch information
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package home | ||
|
||
import ( | ||
"context" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"log" | ||
) | ||
|
||
// bridge | ||
func (me *T) GetBridge(contract string, nonce int32, ch *chan map[string]interface{}) error { | ||
c, err := me.Client.GetCollection(struct{ Collection string }{Collection: "Notification"}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//matchStage := bson.D{ | ||
// {"$match", bson.D{ | ||
// {"operationType", "insert"}, | ||
// //{"fullDocument.index", bson.D{ | ||
// // {"index", 1}, | ||
// //}}, | ||
// }}, | ||
//} | ||
//cs, err := c.Watch(context.TODO(), mongo.Pipeline{matchStage}) //需要开启副本集模式 | ||
|
||
cs, err := c.Watch(context.TODO(), mongo.Pipeline{}) | ||
if err != nil { | ||
return err | ||
} | ||
defer cs.Close(context.TODO()) | ||
// Whenever there is a new change event, decode the change event and print some information about it | ||
for cs.Next(context.TODO()) { | ||
var changeEvent map[string]interface{} | ||
err := cs.Decode(&changeEvent) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fullDocument := changeEvent["fullDocument"].(map[string]interface{}) | ||
contractAdd := fullDocument["contract"] | ||
eventName := fullDocument["eventname"] | ||
if contractAdd == contract { | ||
if eventName == "Withdrawal" || eventName == "Claimable" { | ||
state := fullDocument["state"].(map[string]interface{}) | ||
stateValue := state["value"].(primitive.A) | ||
event := stateValue[0].(map[string]interface{}) | ||
eventNonce := event["value"].(int32) | ||
if nonce == eventNonce { | ||
*ch <- fullDocument | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
return nil | ||
} |