Example #1
0
func TestMultiroom(t *testing.T) {
	roomACounter := 0
	roomBCounter := 0
	da := "hello"
	read := func(rec *gt.Room, c *int) {
		for {
			dat, err := rec.Read()
			if err != nil {
				t.Fatal(err)
				return
			}
			if string(dat) == da {
				*c++
			} else {
				t.Fatal("Wrong data:", string(dat))
			}
		}
	}
	conn, err := gt.Connect("127.0.0.1:8912")
	if err != nil {
		t.Fatal(err)
	}
	conn1, err := gt.Connect("127.0.0.1:8912")
	if err != nil {
		t.Fatal(err)
	}
	roomA := conn.Room("roomA")
	err = roomA.Subscribe()
	if err != nil {
		panic(err)
	}
	roomB := conn.Room("roomB")
	err = roomB.Subscribe()
	if err != nil {
		panic(err)
	}
	go read(roomA, &roomACounter)
	go read(roomB, &roomBCounter)
	sendn := 1000
	for i := 0; i < sendn; i++ {
		conn1.Room("roomA").Publish(da)
	}
	for i := 0; i < sendn; i++ {
		conn1.Room("roomB").Publish(da)
	}
	v := &Waiter{}
	ok := v.Interval(50 * time.Millisecond).Limit(2 * time.Second).Wait(func() bool {
		return roomACounter == sendn && roomBCounter == sendn
	})
	if !ok {
		t.Fatal(roomACounter, roomBCounter)
	}
}
Example #2
0
func TestSameconnSendRec(t *testing.T) {
	roomACounter := 0
	da := "hello"
	read := func(rec *gt.Room, c *int) {
		for {
			dat, err := rec.Read()
			if err != nil {
				t.Fatal(err)
				return
			}
			if string(dat) == da {
				*c++
			} else {
				t.Fatal("Wrong data:", string(dat))
			}
		}
	}
	conn, err := gt.Connect("127.0.0.1:8912")
	if err != nil {
		t.Fatal(err)
	}
	roomA := conn.Room("roomA")
	roomA.Subscribe()
	go read(roomA, &roomACounter)
	conn.Room("roomA").Publish(da)
	v := &Waiter{}
	ok := v.Interval(50 * time.Millisecond).Limit(2 * time.Second).Wait(func() bool {
		return roomACounter == 1
	})
	if !ok {
		t.Fatal(roomACounter)
	}
}
Example #3
0
func TestJson(t *testing.T) {
	roomACounter := 0
	m := map[string]interface{}{
		"key":  "value",
		"key1": "value1",
	}
	da, err := json.Marshal(m)
	if err != nil {
		t.Fatal(err)
	}
	read := func(rec *gt.Room, c *int) {
		for {
			dat, err := rec.Read()
			if err != nil {
				t.Fatal(err)
				return
			}
			var v interface{}
			err = json.Unmarshal(dat, &v)
			if err != nil {
				t.Fatal(err)
			}
			ma := v.(map[string]interface{})
			if len(ma) != 2 || ma["key"] != "value" || ma["key1"] != "value1" {
				t.Fatal(ma)
			}
			*c++
		}
	}
	conn, err := gt.Connect("127.0.0.1:8912")
	if err != nil {
		t.Fatal(err)
	}
	roomA := conn.Room("roomA")
	roomA.Subscribe()
	go read(roomA, &roomACounter)
	conn.Room("roomA").Publish(string(da))
	v := &Waiter{}
	ok := v.Interval(50 * time.Millisecond).Limit(2 * time.Second).Wait(func() bool {
		return roomACounter == 1
	})
	if !ok {
		t.Fatal(roomACounter)
	}
}
Example #4
0
func main() {
	defer mainErr()
	config := config.New()
	config.LoadFromFile()
	dial := config.DBAddr
	if len(config.DBUser) != 0 || len(config.DBPass) != 0 {
		if len(config.DBUser) == 0 {
			panic("Database password provided but username is missing.")
		}
		if len(config.DBPass) == 0 {
			panic("Database username is provided but password is missing.")
		}
		dial = config.DBUser + ":" + config.DBPass + "@" + config.DBAddr
		if !config.DBAdmMode {
			dial = dial + "/" + config.DBName
		}
	}
	fmt.Println("Connecting to db server.")
	session, err := mgo.Dial(dial)
	if err != nil {
		panic(err)
	}
	var conn *gt.Connection
	if len(config.MsgAddr) > 0 {
		fmt.Println("Connecting to msg server.")
		conn, err = gt.Connect(config.MsgAddr)
		if err != nil {
			panic(err)
		}
	}
	db := session.DB(config.DBName)
	defer session.Close()
	fmt.Println("Waiting for websockets connections.")
	http.HandleFunc("/ws/", func(w http.ResponseWriter, req *http.Request) {
		defer printErr(w)
		wsHandler := func(ws *websocket.Conn) {
			defer ws.Close()
			req.URL.Path = "/" + req.URL.Path[3:]
			ctx, err := bang.NewWS(conn, session, db, w, req, config, ws)
			if err != nil {
				fmt.Fprintf(w, err.Error())
				return
			}
			top := top.New(ctx)
			err = top.RouteWS()
			if err != nil {
				fmt.Fprintf(w, err.Error())
			}
		}
		websocket.Handler(wsHandler).ServeHTTP(w, req)
	})
	fmt.Println("Waiting http connections.")
	http.HandleFunc("/",
		func(w http.ResponseWriter, req *http.Request) {
			defer printErr(w)
			ctx, err := bang.New(conn, session, db, w, req, config)
			if err != nil {
				fmt.Fprintf(w, err.Error())
				return
			}
			if ctx == nil {
				return
			}
			top := top.New(ctx)
			err = top.Route()
			if err != nil {
				fmt.Fprintf(w, err.Error())
			}
		})
	err = http.ListenAndServe(config.Addr+":"+config.PortNum, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
}