Skip to content

Commit

Permalink
Merge pull request #245 from Tobeyw/dev-mindy
Browse files Browse the repository at this point in the history
add bridge for ws
  • Loading branch information
Tobeyw authored Mar 20, 2024
2 parents baeade0 + 75c9b81 commit cba42fb
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
31 changes: 30 additions & 1 deletion neo3fura_ws/app/ws/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -140,6 +141,33 @@ func mainpage(w http.ResponseWriter, r *http.Request) {
go ResponseController(mt, wsc, &responseChannel)
}

func bridgepage(w http.ResponseWriter, r *http.Request) {
log2.Infof("DETECT CONNECTION")

params := r.URL.Query()
contract := params.Get("contract")
nonceStr := params.Get("nonce")

nonce, err := strconv.Atoi(nonceStr)
if err != nil {
log2.Fatalf("Failed to convert string to int:%s", err)
}

wsc, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log2.Fatalf("upgrade error:%s", err)
}
mt, _, err := wsc.ReadMessage()
if err != nil {
log2.Fatalf("read message error:%s", err)
}

var responseChannel = make(chan map[string]interface{}, 20)

go c.GetBridge(contract, int32(nonce), &responseChannel)
go ResponseController(mt, wsc, &responseChannel)
}

func ResponseController(mt int, wsc *websocket.Conn, ch *chan map[string]interface{}) {
str := "hello neo3fura"
err := wsc.WriteMessage(mt, []byte(str))
Expand Down Expand Up @@ -180,6 +208,7 @@ func init() {
}

func main() {
http.HandleFunc("/home", mainpage)
go http.HandleFunc("/home", mainpage)
go http.HandleFunc("/bridge", bridgepage)
log2.Fatal(http.ListenAndServe(*add, nil))
}
57 changes: 57 additions & 0 deletions neo3fura_ws/home/ws.Bridge.go
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
}

0 comments on commit cba42fb

Please sign in to comment.