Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions examples/websocket/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
*
* Copyright 2020 waterdrop authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package main

import (
"flag"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"time"

"github.com/gorilla/websocket"
)

var addr = flag.String("addr", "localhost:9000", "http service address")
var ping = []byte("ping")

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: "/ws"}
log.Printf("connecting to %s", u.String())

c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", 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(10 * time.Second)
pingTicker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
defer pingTicker.Stop()

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 <-pingTicker.C:
err := c.WriteMessage(websocket.PingMessage, ping)
if err != nil {
log.Println("write:", err)
return
}
fmt.Println("write ping")
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
}
}
}
66 changes: 66 additions & 0 deletions examples/websocket/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
*
* Copyright 2020 waterdrop authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package main

import (
"fmt"
"github.com/UnderTreeTech/waterdrop/pkg/log"
"github.com/UnderTreeTech/waterdrop/pkg/server/http"
"github.com/gorilla/websocket"
"os"
"os/signal"
"syscall"
"time"
)

var pong = []byte("pong")

func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)

defer log.New(nil).Sync()

srv := http.NewServer(nil)
srv.Upgrade(http.NewWebSocket("/ws", func(ws *http.WebSocket) {
ws.SetPingHandler(func(message string) error {
ws.SetReadDeadline(time.Now().Add(time.Second * 10))
return ws.WriteControl(websocket.PongMessage, pong, time.Now().Add(time.Second))
})
for {
ws.SetReadDeadline(time.Now().Add(time.Second * 10))
msgType, message, err := ws.ReadMessage()
if err != nil {
fmt.Println("read msg fail", err.Error())
break
}
fmt.Println("recv msg", string(message), msgType)

err = ws.WriteMessage(msgType, message)
if err != nil {
fmt.Println("write msg fail", err.Error())
break
}
}
}))

srv.Start()

<-c
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Masterminds/squirrel v1.4.0
github.com/Shopify/sarama v1.27.0
github.com/UnderTreeTech/protobuf v0.0.0-20200819073955-77c6ad3d6345
github.com/alibaba/sentinel-golang v0.6.1 // indirect
github.com/apache/rocketmq-client-go/v2 v2.0.0
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/coreos/go-semver v0.3.0 // indirect
Expand All @@ -23,7 +22,7 @@ require (
github.com/golang/protobuf v1.4.2
github.com/gomodule/redigo v1.8.2
github.com/google/uuid v1.1.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mitchellh/mapstructure v1.3.3
Expand Down
Loading