示例#1
0
文件: wcchat.go 项目: samegoal/wcchat
func (s *chatSession) BackChannelAdd(messageBody []byte) error {
	sm.mutex.Lock()
	defer sm.mutex.Unlock()

	msg := wc.NewMessage(s.bid, messageBody)
	s.bid++
	s.messages = append(s.messages, msg)
	return nil
}
示例#2
0
文件: wcchat.go 项目: samegoal/wcchat
func (s *chatSession) BackChannelNewSessionMessages() error {
	sm.mutex.Lock()
	defer sm.mutex.Unlock()

	for _, session := range sm.sessionMap {
		cs := session.(*chatSession)
		if cs.SID() == s.SID() || cs.user == "" {
			continue
		}
		existingUserMsg := []interface{}{"new", cs.user}
		existingUserJSON, err := json.Marshal(existingUserMsg)
		if err != nil {
			return err
		}
		finalMsg := wc.NewMessage(s.bid, existingUserJSON)
		s.bid++
		s.messages = append(s.messages, finalMsg)
		s.DataNotifier() <- len(finalMsg.Body)
	}
	return nil
}
示例#3
0
文件: wcchat.go 项目: samegoal/wcchat
func (s *chatSession) ForwardChannel(msgs []*wc.Message) error {
	sm.mutex.Lock()
	defer sm.mutex.Unlock()

	for _, msg := range msgs {
		j := map[string]string{}
		err := json.Unmarshal(msg.Body, &j)
		if err != nil {
			return err
		}

		user, hasUser := j["user"]
		message, hasMessage := j["message"]
		bcastMsg := []interface{}{}
		switch {
		case hasUser:
			bcastMsg = []interface{}{"new", user}
			s.user = user
		case hasMessage:
			bcastMsg = []interface{}{"message", s.user, message}
		default:
			return fmt.Errorf("Unsupported message type")
		}
		bcastJSON, err := json.Marshal(bcastMsg)
		if err != nil {
			panic(err)
		}
		for _, bcastSession := range sm.sessionMap {
			cs := bcastSession.(*chatSession)
			finalMsg := wc.NewMessage(cs.bid, bcastJSON)
			cs.bid++
			cs.messages = append(cs.messages, finalMsg)
			cs.DataNotifier() <- len(finalMsg.Body)
		}
	}

	return nil
}
示例#4
0
文件: wcchat.go 项目: samegoal/wcchat
func (sm *chatSessionManager) TerminatedSession(
	s wc.Session,
	reason wc.TerminationReason,
) error {
	sm.mutex.Lock()
	defer sm.mutex.Unlock()

	session := s.(*chatSession)
	delete(sm.sessionMap, session.SID())

	bcastMsg := []interface{}{"delete", session.user}
	bcastJSON, err := json.Marshal(bcastMsg)
	if err != nil {
		panic(err)
	}
	for _, bcastSession := range sm.sessionMap {
		cs := bcastSession.(*chatSession)
		finalMsg := wc.NewMessage(cs.bid, bcastJSON)
		cs.bid++
		cs.messages = append(cs.messages, finalMsg)
		cs.DataNotifier() <- len(finalMsg.Body)
	}
	return nil
}