2019-02-14 02:28:41 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-19 21:49:16 +01:00
|
|
|
"log"
|
2019-02-14 02:28:41 +01:00
|
|
|
"os"
|
2019-02-19 21:49:16 +01:00
|
|
|
"runtime"
|
2019-02-14 02:28:41 +01:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
2019-02-17 03:39:41 +01:00
|
|
|
"github.com/kataras/iris/websocket2"
|
2019-02-14 02:28:41 +01:00
|
|
|
)
|
|
|
|
|
2019-02-19 21:49:16 +01:00
|
|
|
const totalClients = 16000 // max depends on the OS.
|
|
|
|
const http = true
|
2019-02-14 02:28:41 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
ws := websocket.New(websocket.Config{})
|
|
|
|
ws.OnConnection(handleConnection)
|
2019-02-19 21:49:16 +01:00
|
|
|
|
|
|
|
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
|
2019-02-14 02:28:41 +01:00
|
|
|
|
|
|
|
go func() {
|
2019-02-19 21:49:16 +01:00
|
|
|
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
|
2019-02-14 02:28:41 +01:00
|
|
|
for {
|
|
|
|
<-t.C
|
|
|
|
|
2019-02-19 21:49:16 +01:00
|
|
|
n := ws.GetTotalConnections()
|
|
|
|
if n > 0 {
|
|
|
|
started = true
|
2019-02-14 02:28:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:49:16 +01:00
|
|
|
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
|
|
|
|
}
|
2019-02-14 02:28:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-19 21:49:16 +01:00
|
|
|
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)
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
// }
|
|
|
|
|
2019-02-14 02:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-02-19 21:49:16 +01:00
|
|
|
// log.Printf("client [%s] disconnected!\n", c.ID())
|
2019-02-14 02:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleErr(c websocket.Connection, err error) {
|
2019-02-19 21:49:16 +01:00
|
|
|
log.Printf("client [%s] errored: %v\n", c.ID(), err)
|
2019-02-14 02:28:41 +01:00
|
|
|
}
|