mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
add websocket client stress test, passed and update the vendors (this commit fixes the https://github.com/kataras/iris/issues/1178 and https://github.com/kataras/iris/issues/1173)
Former-commit-id: 74ccd8f4bf60a71f1eb0e34149a6f19de95a9148
This commit is contained in:
64
_examples/websocket/go-client-stress-test/server/main.go
Normal file
64
_examples/websocket/go-client-stress-test/server/main.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
const totalClients = 1200
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
|
||||
ws := websocket.New(websocket.Config{})
|
||||
ws.OnConnection(handleConnection)
|
||||
app.Get("/socket", ws.Handler())
|
||||
|
||||
go func() {
|
||||
t := time.NewTicker(2 * time.Second)
|
||||
for {
|
||||
<-t.C
|
||||
|
||||
conns := ws.GetConnections()
|
||||
for _, conn := range conns {
|
||||
// fmt.Println(conn.ID())
|
||||
// Do nothing.
|
||||
_ = conn
|
||||
}
|
||||
|
||||
if atomic.LoadUint64(&count) == totalClients {
|
||||
fmt.Println("ALL CLIENTS DISCONNECTED SUCCESSFULLY.")
|
||||
t.Stop()
|
||||
os.Exit(0)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func handleConnection(c websocket.Connection) {
|
||||
c.OnError(func(err error) { handleErr(c, err) })
|
||||
c.OnDisconnect(func() { handleDisconnect(c) })
|
||||
c.On("chat", func(message string) {
|
||||
c.To(websocket.Broadcast).Emit("chat", c.ID()+": "+message)
|
||||
})
|
||||
}
|
||||
|
||||
var count uint64
|
||||
|
||||
func handleDisconnect(c websocket.Connection) {
|
||||
atomic.AddUint64(&count, 1)
|
||||
fmt.Printf("client [%s] disconnected!\n", c.ID())
|
||||
}
|
||||
|
||||
func handleErr(c websocket.Connection, err error) {
|
||||
fmt.Printf("client [%s] errored: %v\n", c.ID(), err)
|
||||
}
|
||||
Reference in New Issue
Block a user