Ejemplo n.º 1
0
func main() {
	EventCenter := event_center.NewClassicEventCenter("GoGapEventCenter")

	EventCenter.RegisterEvent(event_center.ConcurrencyMode,
		EVENT_CMD_STOP,
		EVENT_RECEIVER_STOPPED,
		EVENT_TRANS_MSG,
	)

	//EVENT_RECEIVER_STOPPED 事件响应
	stopSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {
		fmt.Println("::stopSubscriber")
		fmt.Println(values)
		fmt.Println()
	})
	EventCenter.Subscribe(EVENT_CMD_STOP, stopSubscriber)

	//EVENT_TRANS_MSG 事件响应
	transMsgSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {
		fmt.Println("::trans msg")
		fmt.Println(values)
		if reflect.TypeOf(values[0]).Kind() == reflect.Func {
			fmt.Println("params is function")
			callBack := reflect.ValueOf(values[0])
			in := make([]reflect.Value, 0)
			go callBack.Call(in)
		}
		fmt.Println()
	})
	EventCenter.Subscribe(EVENT_TRANS_MSG, transMsgSubscriber)

	for {
		time.Sleep(time.Second)

		EventCenter.PushEvent(EVENT_CMD_STOP, "SSSS")

		callBackFunc := func() interface{} {
			fmt.Println("call back function")
			return true
		}
		EventCenter.PushEvent(EVENT_TRANS_MSG, callBackFunc, "hahaha", "hello", "word")
		fmt.Println("In Main Program")

		time.Sleep(time.Second)
		fmt.Println()
		fmt.Println()
		fmt.Println("---------^---------")
	}
}
Ejemplo n.º 2
0
package spirit

import (
	"github.com/gogap/event_center"
)

var (
	EventCenter event_center.EventCenter = event_center.NewClassicEventCenter(SPIRIT)
)

const (
	EVENT_CMD_STOP = "EVENT_CMD_STOP"

	EVENT_RECEIVER_STOPPED           = "EVENT_RECEIVER_STOPPED"
	EVENT_RECEIVER_STARTED           = "EVENT_RECEIVER_STARTED"
	EVENT_RECEIVER_MSG_COUNT_UPDATED = "EVENT_RECEIVER_MSG_COUNT_UPDATED"

	EVENT_RECEIVER_MSG_RECEIVED = "EVENT_RECEIVER_MSG_RECEIVED"
	EVENT_RECEIVER_MSG_ERROR    = "EVENT_RECEIVER_MSG_ERROR"
	EVENT_RECEIVER_MSG_DELETED  = "EVENT_RECEIVER_MSG_DELETED"

	EVENT_HEARTBEAT = "EVENT_HEARTBEAT"

	EVENT_BEFORE_MESSAGE_HOOK = "EVENT_BEFORE_MESSAGE_HOOK"
	EVENT_AFTER_MESSAGE_HOOK  = "EVENT_AFTER_MESSAGE_HOOK"

	EVENT_BEFORE_MESSAGE_SEND = "EVENT_BEFORE_MESSAGE_SEND"
	EVENT_AFTER_MESSAGE_SEND  = "EVENT_AFTER_MESSAGE_SEND"

	EVENT_BASE_COMPONENT_STOPPING = "EVENT_BASE_COMPONENT_STOPPING"
	EVENT_BASE_COMPONENT_STOPPED  = "EVENT_BASE_COMPONENT_STOPPED"