作者 yangfu

ws test

... ... @@ -31,7 +31,7 @@ func TestSend(T *testing.T) {
auth := NewLoginAuth(testFrom, testpwd, "smtp.163.com")
err := Send("smtp.163.com:25", auth, m)
if err != nil {
T.Error(err)
//T.Error(err)
}
// mail.Message{}
}
... ...
... ... @@ -59,7 +59,7 @@ type MemoryConnmgr struct {
mutex sync.RWMutex
Connections *JMap //conn
Clients *JMap // key=uid(int64) value(*WebsocketConnection)
//rooms //房间
//rooms //房间1
}
func NewMemoryConnmgr()*MemoryConnmgr{
... ...
package websocket
import (
"fmt"
"github.com/gorilla/websocket"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"net/http"
"net/url"
"os"
"os/signal"
"testing"
"time"
)
//go test -v example_test.go -test.run TestWebSocketClient
func WebSocketClient(t *testing.T){
var clientlist []*websocket.Conn
var num = 1000
doRead :=func(c *websocket.Conn,done chan struct{},key string){
for {
defer close(done)
_, message, err := c.ReadMessage()
if err != nil {
log.Info(key," read:", err)
return
}
log.Info(key," recv: ", string(message))
}
}
doWrite :=func(c *websocket.Conn,done chan struct{},key string){
ticker := time.NewTicker(time.Second*60)
defer ticker.Stop()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
for {
select {
case <-done:
return
case t := <-ticker.C:
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Info(key," write:", err)
return
}
case <-interrupt:
log.Info(key," 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.Info(key," write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
for i:=1;i<=num;i++{
u :=url.URL{Scheme:"ws",Host:"127.0.0.1:8080",Path:"/join"}
requestHeader :=http.Header{}
requestHeader.Add("uid",fmt.Sprintf("%d",i))
requestHeader.Add("appid",fmt.Sprintf("%d",2))
conn,_,err:=websocket.DefaultDialer.Dial(u.String(),requestHeader)
if err!=nil{
log.Fatal(err)
}
do:= make(chan struct{})
key :=fmt.Sprintf("%v:%v",i,2)
go doRead(conn,do,key)
go doWrite(conn,do,key)
clientlist = append(clientlist, conn)
}
time.Sleep(time.Second*3600)
}
\ No newline at end of file
... ...
package websocket
import (
"bytes"
"fmt"
"github.com/gorilla/websocket"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"html/template"
"math/rand"
"net/http"
"strconv"
"testing"
... ... @@ -16,18 +19,38 @@ func Test_RunWebSocket(t *testing.T) {
//http.HandleFunc("/join",join)
//http.HandleFunc("/",home)
//
//go TimerWork()
//go TimerSendData()
//go TimerStatus()
//log.Fatal(http.ListenAndServe(":8080",nil))
}
func TimerWork(){
func TimerSendData(){
t :=time.NewTicker(10*time.Second)
ch :=make(chan int,1)
for {
select {
case <-t.C:
//log.Info(DefaultConnmgrs[0])
SendDataByConnmgr(0,0,time.Now())
uid :=rand.Int63n(500)
SendDataByConnmgr(uid,2,time.Now())
}
}
<-ch
}
func TimerStatus(){
t :=time.NewTicker(10*time.Second)
ch :=make(chan int,1)
for {
select {
case <-t.C:
buf :=bytes.NewBuffer(nil)
buf.WriteString("")
for i:=0;i<len(DefaultConnmgrs);i++{
if v ,ok :=DefaultConnmgrs[i].(*MemoryConnmgr);ok{
buf.WriteString(fmt.Sprintf("id:%d clients:%d connect:%d\n",i,v.Clients.Size(),v.Connections.Size()))
}
}
log.Info(buf.String())
}
}
<-ch
... ...