Exemple #1
0
func (self *Server) Start() error {
	self.s.Listen(self.config.Port)
	defer self.Close()

	<-self.start
	fmt.Println("server start")

	for {
		conn, err := self.s.Accept()
		if err != nil {
			fmt.Println(err)
			return err
		} else {

			genClientID := func() string {
				self.joinedNumber++
				return fmt.Sprintf("%s.%d", self.config.ServerName, self.joinedNumber)
			}

			cid := genClientID()

			fmt.Println("client id :" + cid)
			tempClient := client.CreateClient(conn, cid)
			self.d.Pending <- tempClient

			go func(c *client.Client) {
				defer func() {
					self.d.Quiting <- c
				}()

				for {
					rawData, ok := c.GetIncoming()
					if !ok {
						break
					}

					fmt.Println(rawData)

					err := self.d.MsgManager.PutMessage(
						message.NewMessage(
							self.config.Pt, string(cid), "", rawData, daemon.MESSAGE_TYPE_TOSERVICE))

					if err != nil {
						fmt.Sprintf("server.error|msg:%s", err)
						self.d.MsgManager.PutMessage(
							daemon.NewSimpleMessage(
								string(cid),
								fmt.Sprintf("server.error|msg:%s", err)))
					}
				}
			}(tempClient)
		}
	}
}
Exemple #2
0
func GetUserName(cid string, originalMsg *message.Message) (username string) {
	request := message.NewMessage(nil, "chatroom", "auth",
		map[string]string{"command": "getusername", "cid": cid},
		daemon.MESSAGE_TYPE_TOSERVICE)
	message.CopySession(originalMsg, request)
	hazel.SendMessage(request)
	select {
	case msg := <-request.Response:
		return msg.Params["username"]
	case <-time.After(time.Second * 2):
		return "匿名(auth time out)"
	}
}
Exemple #3
0
func NewSimpleMessage(des string, msg string) *message.Message {
	params := map[string]string{"msg": msg}
	return message.NewMessage(&protocol.SimpleProtocol{},
		"", des, params, MESSAGE_TYPE_TOCLIENT)
}