Пример #1
0
func (service *Service) handle_message(context *gowsev.Context, id uint64, message string) {
	var channel, body string

	_, err := fmt.Sscanf(message, "subscribe:%s", &channel)
	if err == nil {
		context.Write(id, []byte("OK"))
		service.subscribe(id, channel)
		return
	}

	_, err = fmt.Sscanf(message, "unsubscribe:%s", &channel)
	if err == nil {
		context.Write(id, []byte("OK"))
		service.unsubscribe(id, channel)
		return
	}

	if message == "unsubscribe-all" {
		context.Write(id, []byte("OK"))
		service.unsubscribe_all(id)

		return
	}

	components := strings.SplitN(message, ":", 3)
	if len(components) >= 3 && components[0] == "publish" {
		context.Write(id, []byte("OK"))
		channel = components[1]
		body = components[2]
		service.publish(context, channel, body)
		return
	}

	context.Write(id, []byte("Invalid message"))
}
Пример #2
0
func (service *Service) publish(context *gowsev.Context, ch string, message string) {
	ids, ok := service.chIdMap[ch]
	if ok {
		for id, _ := range ids {
			context.Write(id, []byte(message))
		}
	}
}