mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
improve client test, I think we are OK, both gorilla(websocket) and ws(websocket2) have the same API, it's time to combine them but first let's give a lower level of api available for users if they want to manage the routines by theirselves (i.e on unix they can use netpolls manually)
Former-commit-id: 3209a7490939bce913732c1375190b0771ba63ae
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -10,38 +11,84 @@ import (
|
||||
"github.com/kataras/iris/websocket2"
|
||||
)
|
||||
|
||||
const totalClients = 100000
|
||||
const totalClients = 16000 // max depends on the OS.
|
||||
const http = true
|
||||
|
||||
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())
|
||||
|
||||
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
|
||||
|
||||
go func() {
|
||||
t := time.NewTicker(2 * time.Second)
|
||||
dur := 8 * time.Second
|
||||
if totalClients >= 64000 {
|
||||
// if more than 64000 then let's no check every 8 seconds, let's do it every 24 seconds,
|
||||
// just for simplicity, either way works.
|
||||
dur = 24 * time.Second
|
||||
}
|
||||
t := time.NewTicker(dur)
|
||||
defer t.Stop()
|
||||
defer os.Exit(0)
|
||||
defer runtime.Goexit()
|
||||
|
||||
var started bool
|
||||
for {
|
||||
<-t.C
|
||||
|
||||
conns := ws.GetConnections()
|
||||
for _, conn := range conns {
|
||||
// fmt.Println(conn.ID())
|
||||
// Do nothing.
|
||||
_ = conn
|
||||
n := ws.GetTotalConnections()
|
||||
if n > 0 {
|
||||
started = true
|
||||
}
|
||||
|
||||
if atomic.LoadUint64(&count) == totalClients {
|
||||
fmt.Println("ALL CLIENTS DISCONNECTED SUCCESSFULLY.")
|
||||
t.Stop()
|
||||
os.Exit(0)
|
||||
return
|
||||
if started {
|
||||
totalConnected := atomic.LoadUint64(&count)
|
||||
|
||||
if totalConnected == totalClients {
|
||||
if n != 0 {
|
||||
log.Println("ALL CLIENTS DISCONNECTED BUT LEFTOVERS ON CONNECTIONS LIST.")
|
||||
} else {
|
||||
log.Println("ALL CLIENTS DISCONNECTED SUCCESSFULLY.")
|
||||
}
|
||||
return
|
||||
} else if n == 0 {
|
||||
log.Printf("%d/%d CLIENTS WERE NOT CONNECTED AT ALL. CHECK YOUR OS NET SETTINGS. ALL OTHER CONNECTED CLIENTS DISCONNECTED SUCCESSFULLY.\n",
|
||||
totalClients-totalConnected, totalClients)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
if http {
|
||||
app := iris.New()
|
||||
app.Get("/", ws.Handler())
|
||||
app.Run(iris.Addr(":8080"))
|
||||
return
|
||||
}
|
||||
|
||||
// ln, err := net.Listen("tcp", ":8080")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// defer ln.Close()
|
||||
// for {
|
||||
// conn, err := ln.Accept()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// go func() {
|
||||
// err = ws.HandleConn(conn)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// }()
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
func handleConnection(c websocket.Connection) {
|
||||
@@ -56,9 +103,9 @@ var count uint64
|
||||
|
||||
func handleDisconnect(c websocket.Connection) {
|
||||
atomic.AddUint64(&count, 1)
|
||||
fmt.Printf("client [%s] disconnected!\n", c.ID())
|
||||
// log.Printf("client [%s] disconnected!\n", c.ID())
|
||||
}
|
||||
|
||||
func handleErr(c websocket.Connection, err error) {
|
||||
fmt.Printf("client [%s] errored: %v\n", c.ID(), err)
|
||||
log.Printf("client [%s] errored: %v\n", c.ID(), err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user