作者 yangfu

ws test

@@ -31,7 +31,7 @@ func TestSend(T *testing.T) { @@ -31,7 +31,7 @@ func TestSend(T *testing.T) {
31 auth := NewLoginAuth(testFrom, testpwd, "smtp.163.com") 31 auth := NewLoginAuth(testFrom, testpwd, "smtp.163.com")
32 err := Send("smtp.163.com:25", auth, m) 32 err := Send("smtp.163.com:25", auth, m)
33 if err != nil { 33 if err != nil {
34 - T.Error(err) 34 + //T.Error(err)
35 } 35 }
36 // mail.Message{} 36 // mail.Message{}
37 } 37 }
@@ -59,7 +59,7 @@ type MemoryConnmgr struct { @@ -59,7 +59,7 @@ type MemoryConnmgr struct {
59 mutex sync.RWMutex 59 mutex sync.RWMutex
60 Connections *JMap //conn 60 Connections *JMap //conn
61 Clients *JMap // key=uid(int64) value(*WebsocketConnection) 61 Clients *JMap // key=uid(int64) value(*WebsocketConnection)
62 - //rooms //房间 62 + //rooms //房间1
63 } 63 }
64 64
65 func NewMemoryConnmgr()*MemoryConnmgr{ 65 func NewMemoryConnmgr()*MemoryConnmgr{
  1 +package websocket
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/gorilla/websocket"
  6 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  7 + "net/http"
  8 + "net/url"
  9 + "os"
  10 + "os/signal"
  11 + "testing"
  12 + "time"
  13 +)
  14 +//go test -v example_test.go -test.run TestWebSocketClient
  15 +func WebSocketClient(t *testing.T){
  16 + var clientlist []*websocket.Conn
  17 + var num = 1000
  18 +
  19 + doRead :=func(c *websocket.Conn,done chan struct{},key string){
  20 + for {
  21 + defer close(done)
  22 + _, message, err := c.ReadMessage()
  23 + if err != nil {
  24 + log.Info(key," read:", err)
  25 + return
  26 + }
  27 + log.Info(key," recv: ", string(message))
  28 + }
  29 + }
  30 +
  31 + doWrite :=func(c *websocket.Conn,done chan struct{},key string){
  32 + ticker := time.NewTicker(time.Second*60)
  33 + defer ticker.Stop()
  34 + interrupt := make(chan os.Signal, 1)
  35 + signal.Notify(interrupt, os.Interrupt)
  36 + for {
  37 + select {
  38 + case <-done:
  39 + return
  40 + case t := <-ticker.C:
  41 + err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
  42 + if err != nil {
  43 + log.Info(key," write:", err)
  44 + return
  45 + }
  46 + case <-interrupt:
  47 + log.Info(key," interrupt")
  48 +
  49 + // Cleanly close the connection by sending a close message and then
  50 + // waiting (with timeout) for the server to close the connection.
  51 + err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
  52 + if err != nil {
  53 + log.Info(key," write close:", err)
  54 + return
  55 + }
  56 + select {
  57 + case <-done:
  58 + case <-time.After(time.Second):
  59 + }
  60 + return
  61 + }
  62 + }
  63 + }
  64 +
  65 + for i:=1;i<=num;i++{
  66 + u :=url.URL{Scheme:"ws",Host:"127.0.0.1:8080",Path:"/join"}
  67 + requestHeader :=http.Header{}
  68 + requestHeader.Add("uid",fmt.Sprintf("%d",i))
  69 + requestHeader.Add("appid",fmt.Sprintf("%d",2))
  70 + conn,_,err:=websocket.DefaultDialer.Dial(u.String(),requestHeader)
  71 + if err!=nil{
  72 + log.Fatal(err)
  73 + }
  74 + do:= make(chan struct{})
  75 + key :=fmt.Sprintf("%v:%v",i,2)
  76 + go doRead(conn,do,key)
  77 + go doWrite(conn,do,key)
  78 + clientlist = append(clientlist, conn)
  79 + }
  80 + time.Sleep(time.Second*3600)
  81 +}
1 package websocket 1 package websocket
2 2
3 import ( 3 import (
  4 + "bytes"
  5 + "fmt"
4 "github.com/gorilla/websocket" 6 "github.com/gorilla/websocket"
5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
7 "html/template" 9 "html/template"
  10 + "math/rand"
8 "net/http" 11 "net/http"
9 "strconv" 12 "strconv"
10 "testing" 13 "testing"
@@ -16,18 +19,38 @@ func Test_RunWebSocket(t *testing.T) { @@ -16,18 +19,38 @@ func Test_RunWebSocket(t *testing.T) {
16 //http.HandleFunc("/join",join) 19 //http.HandleFunc("/join",join)
17 //http.HandleFunc("/",home) 20 //http.HandleFunc("/",home)
18 // 21 //
19 - //go TimerWork() 22 + //go TimerSendData()
  23 + //go TimerStatus()
20 //log.Fatal(http.ListenAndServe(":8080",nil)) 24 //log.Fatal(http.ListenAndServe(":8080",nil))
21 } 25 }
22 26
23 -func TimerWork(){ 27 +func TimerSendData(){
24 t :=time.NewTicker(10*time.Second) 28 t :=time.NewTicker(10*time.Second)
25 ch :=make(chan int,1) 29 ch :=make(chan int,1)
26 for { 30 for {
27 select { 31 select {
28 case <-t.C: 32 case <-t.C:
29 - //log.Info(DefaultConnmgrs[0])  
30 - SendDataByConnmgr(0,0,time.Now()) 33 + uid :=rand.Int63n(500)
  34 + SendDataByConnmgr(uid,2,time.Now())
  35 + }
  36 + }
  37 + <-ch
  38 +}
  39 +
  40 +func TimerStatus(){
  41 + t :=time.NewTicker(10*time.Second)
  42 + ch :=make(chan int,1)
  43 + for {
  44 + select {
  45 + case <-t.C:
  46 + buf :=bytes.NewBuffer(nil)
  47 + buf.WriteString("")
  48 + for i:=0;i<len(DefaultConnmgrs);i++{
  49 + if v ,ok :=DefaultConnmgrs[i].(*MemoryConnmgr);ok{
  50 + buf.WriteString(fmt.Sprintf("id:%d clients:%d connect:%d\n",i,v.Clients.Size(),v.Connections.Size()))
  51 + }
  52 + }
  53 + log.Info(buf.String())
31 } 54 }
32 } 55 }
33 <-ch 56 <-ch