feat: safe websocket client implementation
This commit is contained in:
25
v1/examples/client/main.go
Normal file
25
v1/examples/client/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
wsClient, err := client.NewSafeWebsocketClientBuilder().
|
||||
BaseHost("localhost").
|
||||
BasePort(8080).
|
||||
Path("/ws/test/data_1").
|
||||
UseTLS(false).
|
||||
Build()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
dataChannel := wsClient.DataChannel()
|
||||
|
||||
for data := range dataChannel {
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
}
|
||||
52
v1/examples/server/main.go
Normal file
52
v1/examples/server/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.neurocipta.com/rogerferdinan/safe-web-socket/v1/server"
|
||||
)
|
||||
|
||||
type ExampleData struct {
|
||||
Time time.Time `json:"time"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
s, err := server.NewSafeWebsocketServerBuilder().
|
||||
BaseHost("localhost").
|
||||
BasePort(8080).
|
||||
HandleFuncWebsocket("/ws/test/", "data_1", func(c chan []byte) {
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
for range ticker.C {
|
||||
jsonBytes, err := json.Marshal(ExampleData{
|
||||
Time: time.Now(),
|
||||
Data: "data_1",
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
c <- jsonBytes
|
||||
}
|
||||
}).
|
||||
HandleFuncWebsocket("/ws/test/", "data_2", func(c chan []byte) {
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
for range ticker.C {
|
||||
jsonBytes, err := json.Marshal(ExampleData{
|
||||
Time: time.Now(),
|
||||
Data: "data_2",
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
c <- jsonBytes
|
||||
}
|
||||
}).
|
||||
Build()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
s.ListenAndServe()
|
||||
}
|
||||
Reference in New Issue
Block a user