Beispiel #1
0
func TestServer2(t *testing.T) {
	go func() {
		once.Do(startServer2)
	}()

	//	time.Sleep(5 * time.Second)

	conn, err := newClient()
	defer conn.Close()

	if err != nil {
		log.Error("newClient: ", err.Error())
		return
	}

	var msg string

	//	log.Info("test:register")
	//	msg = []byte(`{"cmd":100,
	//		"content":"{\"account\":\"testUtf\",\"psw\":\"pswlk22\"}"
	//		}`)
	//	sendMsg(conn, msg)
	//	time.Sleep(1 * time.Second)

	log.Info("test:cmdLoginReq1")
	//	msg = []byte(`{"cmd":104,"content":"{\"account\":\"testUtf\",\"psw\":\"pswlk22\"}"}`)
	msg = com.MakeMsgString(com.Cmd_Ag_LoginReq, 0, &LoginReq{"testUtf", "pswlk22"})
	sendMsg(conn, msg)
	time.Sleep(1 * time.Second)

	//	log.Info("test:cmdLoginReq2")
	//	msg = []byte(`{"cmd":104,"content":"{\"account\":\"testUtf\",\"psw\":\"p\"}"}`)
	//	sendMsg(conn, msg)
	//	time.Sleep(1 * time.Second)

	log.Info("test:handleInfo")
	//	msg = []byte(`{"cmd":106}`)
	//	msg = []byte(`{"cmd":106,"content":"{\"account\":\"testUtf\",\"psw\":\"p\"}"}`)
	msg = com.MakeMsgString(com.Cmd_Ag_InfoReq, 0, nil)
	sendMsg(conn, msg)
	time.Sleep(11 * time.Second)

	log.Info("test:enter game room")
	msg = com.MakeMsgString(com.Cmd_Ag_ToGameReq, 0, nil)

}
Beispiel #2
0
func startServer1() {
	serve := func(ws *websocket.Conn) {
		fmt.Printf("agent come")
		agent := fw.NewAgent(&model{}, fw.NewWsReadWriter(ws), 1)
		agent.Serve()
	}

	http.Handle("/agent", websocket.Handler(serve))
	//	http.ListenAndServe(":8000", nil)

	server := httptest.NewServer(nil)
	serverAddr = server.Listener.Addr().String()
	log.Info("Test WebSocket server listening on ", serverAddr)
}
Beispiel #3
0
func stringerRun(stringer, dir, typeName, fileName string) {
	log.Infof("run: file=%s type=%s", fileName, typeName)
	source := filepath.Join(dir, fileName)
	//	err := copy(source, filepath.Join("testdata", fileName))
	//	if err != nil {
	//		log.Fatalf("copying file to temporary directory: %s", err)
	//	}
	stringSource := filepath.Join(dir, typeName+"_string.go")
	// Run stringer in temporary directory.
	log.Info(stringSource)
	err := run(stringer, "-type", typeName, "-output", stringSource, source)
	if err != nil {
		log.Fatal(err)
	}
}
Beispiel #4
0
func main() {
	//register rpc
	models := dao.NewModels()
	rpc.Register(models.User)
	rpc.Register(models.Game)
	defer func() {
		models.Exit()
	}()

	//network
	serverAddr := "127.0.0.1:" + cfg.DaoPort
	l, e := net.Listen("tcp", serverAddr) // any available address
	if e != nil {
		log.Fatalf("net.Listen tcp : %v", e)
	}
	log.Info("dao RPC server listening on ", serverAddr)
	rpc.Accept(l)
}
Beispiel #5
0
func NewUser(p *Models) *User {
	u := new(User)
	c, err := redis.Dial("tcp", cfg.RedisAddr(),
		redis.DialReadTimeout(1*time.Second), redis.DialWriteTimeout(1*time.Second))
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	u.c = c

	//select db
	s, err := c.Do("SELECT", cfg.RedisDBs[cfg.Pf])
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	//temp
	fw.PrintType(s, "s")

	//fill keys
	b, _ := redis.Bool(c.Do("EXISTS", k_account_count))

	//	switch b.(type) {
	//	case interface{}:
	//		log..Debug("interface")
	//	case []byte:
	//		log..Debug("byte")
	//	case string:
	//		log..Debug("string")
	//	case *int:
	//		log..Debug("int")
	//	default:
	//		log..Debug("other")
	//	}

	if b == false {
		c.Do("SET", k_account_count, 0)
		log.Info("fill key:", k_account_count)
	}

	//register model
	u.parent = p
	return u
}
Beispiel #6
0
func startServer2() {
	server, err := NewServer()
	if err != nil {
		log.Error(err.Error())
		return
	}
	defer func() {
		server.Close()
	}()

	serve := func(ws *websocket.Conn) {
		log.Debug("new agent")
		if err := server.Serve(fw.NewWsReadWriter(ws)); err != nil {
			log.Error(err.Error())
		}
		log.Debug("agent leave")
	}

	http.Handle("/", websocket.Handler(serve))
	http.ListenAndServe(cfg.AgentAddr(), nil)
	log.Info("server2 serve on ", cfg.AgentAddr())
}
Beispiel #7
0
func TestServer1(t *testing.T) {
	once.Do(startServer1)

	// websocket.Dial()
	client, err := net.Dial("tcp", serverAddr)
	if err != nil {
		t.Fatal("dialing", err)
	}
	log.Info("t=%v", t)
	conn, err := websocket.NewClient(newConfig_("/agent"), client)
	if err != nil {
		t.Errorf("WebSocket handshake error: %v", err)
		return
	}

	for i := 0; i < 10; i++ {
		msg := []byte("hello, world")
		fw.PrintType(msg, "msg")
		msg = append(msg, byte(i))
		//		append(msg, []byte("\n"))
		if _, err := conn.Write(msg); err != nil {
			t.Errorf("Write: %v", err)
		}
		var actual_msg = make([]byte, 512)
		n, err := conn.Read(actual_msg)
		if err != nil {
			t.Errorf("Read: %v", err)
		}
		actual_msg = actual_msg[0:n]
		if !bytes.Equal(msg, actual_msg) {
			t.Logf("Test: send %q got %q", msg, actual_msg)
		}
	}

	conn.Close()
}
Beispiel #8
0
func (c *Clientmodel) handleInfo(content string) (err error) {
	log.Info("handleInfo")
	return
}
Beispiel #9
0
func (c *Clientmodel) handleRegister(content string) (err error) {
	log.Info("register")
	return
}
Beispiel #10
0
func main() {
	cfg.FlushCfgToDB()
	log.Info("FlushCfgToDB")
}