-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sui/watcher: updates to python example * sui/watcher: simple go example
- Loading branch information
1 parent
0aedc11
commit 16dfd94
Showing
6 changed files
with
89 additions
and
76 deletions.
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= | ||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0= | ||
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= |
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 |
---|---|---|
@@ -1,85 +1,35 @@ | ||
// Copyright 2015 The Gorilla WebSocket Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build ignore | ||
// +build ignore | ||
|
||
// go get github.com/gorilla/websocket | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"golang.org/x/net/websocket" | ||
"log" | ||
"net/url" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"os" | ||
) | ||
|
||
var addr = flag.String("addr", "localhost:5001", "http service address") | ||
|
||
func main() { | ||
flag.Parse() | ||
log.SetFlags(0) | ||
|
||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt) | ||
|
||
u := url.URL{Scheme: "ws", Host: *addr, Path: "/sui_subscribeEvent"} | ||
log.Printf("connecting to %s", u.String()) | ||
|
||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) | ||
origin := "http://localhost/" | ||
url := "ws://localhost:9001" | ||
ws, err := websocket.Dial(url, "", origin) | ||
if err != nil { | ||
log.Fatal("dial:", err) | ||
log.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
done := make(chan struct{}) | ||
|
||
go func() { | ||
defer close(done) | ||
for { | ||
_, message, err := c.ReadMessage() | ||
if err != nil { | ||
log.Println("read:", err) | ||
return | ||
} | ||
log.Printf("recv: %s", message) | ||
} | ||
}() | ||
|
||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
s := fmt.Sprintf(`{"jsonrpc":"2.0", "id": 1, "method": "sui_subscribeEvent", "params": [{"SenderAddress": "%s"}]}`, os.Getenv("WORM_OWNER")) | ||
fmt.Printf("Sending: %s.\n", s) | ||
|
||
if _, err := ws.Write([]byte(s)); err != nil { | ||
log.Fatal(err) | ||
} | ||
for { | ||
select { | ||
case <-done: | ||
return | ||
case t := <-ticker.C: | ||
err := c.WriteMessage(websocket.TextMessage, []byte(t.String())) | ||
if err != nil { | ||
log.Println("write:", err) | ||
return | ||
} | ||
case <-interrupt: | ||
log.Println("interrupt") | ||
|
||
// Cleanly close the connection by sending a close message and then | ||
// waiting (with timeout) for the server to close the connection. | ||
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) | ||
if err != nil { | ||
log.Println("write close:", err) | ||
return | ||
} | ||
select { | ||
case <-done: | ||
case <-time.After(time.Second): | ||
} | ||
return | ||
var msg = make([]byte, 512) | ||
var n int | ||
ws.SetReadDeadline(time.Now().Local().Add(1_000_000_000)); | ||
if n, err = ws.Read(msg); err != nil { | ||
fmt.Printf("err"); | ||
} else { | ||
fmt.Printf("Received: %s.\n", msg[:n]) | ||
} | ||
} | ||
} |
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