func New(name string) *Channel { c := new(Channel) c.name = name c.eventHandler = emission.NewEmitter() return c }
func NewApp() *App { app := &App{ Emitter: emission.NewEmitter(), Handlers: make(map[string]func(w http.ResponseWriter, r *http.Request)), ConnManager: make(map[string]*Conn), RoomManager: make(map[string]*Room), } return app }
func init() { // Create a new emitter, set the recover function and the max listeners. emitter = emission.NewEmitter(). RecoverWith(recoverEmitter). SetMaxListeners(emitterMaxListeners) // Start the event loops on init. bitmonster.OnInit(func() { go listenForUserChangesLoop() }) }
func main() { emitter := emission.NewEmitter() emitter.SetMaxListeners(-1) go subscribe(emitter) handler := socketHandler(emitter) http.Handle("/", handler) http.ListenAndServe(":5000", nil) }
// onNewSocket is triggered as soon as a new socket connects. func onNewSocket(s *glue.Socket) { // Create the BitMonster Socket value. socket := &Socket{ socket: s, chanCall: s.Channel(channelCall), chanEvent: s.Channel(channelEvent), values: make(map[interface{}]interface{}), valueTimeouts: make(map[interface{}]*time.Timer), } // Save itself to the glue Value. s.Value = socket // Set the emitter. // Create a new emitter, set the recover function and the max listeners. socket.emitter = emission.NewEmitter(). RecoverWith(recoverEmitter). SetMaxListeners(emitterMaxListeners) // We won't read any data from the socket itself. // Discard the received data! s.DiscardRead() // Set the function which is triggered as soon as the socket is closed. s.OnClose(func() { onCloseSocket(socket) }) // Prepare the call channel. socket.chanCall.OnRead(func(data string) { // Trigger the call request. handleCallRequest(socket, data) }) // Prepare the event channel. socket.chanEvent.OnRead(func(data string) { // Trigger the event request. handleEventRequest(socket, data) }) // Trigger the event. emitter.Emit(emitterOnNewSocket, socket) }
func main() { emitter := emission.NewEmitter() hello := func(to string) { fmt.Printf("Hello %s!\n", to) } count := func(count int) { for i := 0; i < count; i++ { fmt.Println(i) } } emitter.On("hello", hello). On("count", count). Emit("hello", "world"). Emit("count", 5) emitter.Emit("hello", "sekai") }
func init() { // Create a new emitter, set the recover function and the max listeners. emitter = emission.NewEmitter(). RecoverWith(recoverEmitter). SetMaxListeners(emitterMaxListeners) }
// NewNormalizedEmitter constructor func NewNormalizedEmitter() *NormalizedEmitter { return &NormalizedEmitter{Emitter: emission.NewEmitter()} }
func main() { tm.Init() tm.Clear(tm.ColorDefault, tm.ColorDefault) gameOver := false playerWon := false emitter := emission.NewEmitter() emitter.On(event.EVENT_KEY_CTRL_C, func() { gameOver = true }) emitter.On(event.EVENT_PLAYER_WINS, func() { playerWon = true gameOver = true }) emitter.On(event.EVENT_PLAYER_LOSES, func() { gameOver = true }) keyboard := new(keyboard.Keyboard) keyboard.Init(emitter) mainboard := new(gameboard.Mainboard) mainboard.Init(1, 1, 50, 20) player := new(player.Player) player.Init(emitter, 1, 50, 20) alienManager := new(alien.AlienManager) alienManager.Init(emitter, 1, 1, 50, 20) var currentTime syscall.Timeval eventQueue := make(chan tm.Event) go func() { for { eventQueue <- tm.PollEvent() } }() timer := time.NewTimer(time.Duration(time.Millisecond * 60)) for gameOver == false { select { case ev := <-eventQueue: if ev.Type == tm.EventKey { keyboard.HandleEvent(ev.Key) } case <-timer.C: syscall.Gettimeofday(¤tTime) emitter.Emit(event.EVENT_HEARTBEAT, (int64(currentTime.Sec)*1e3 + int64(currentTime.Usec)/1e3)) timer.Reset(time.Duration(time.Millisecond * 60)) } tm.Flush() } tm.Close() if playerWon == true { fmt.Println("You win!") } else { fmt.Println("You lose :(") } }
var upgrader = websocket.Upgrader{ ReadBufferSize: 4096, WriteBufferSize: 4096, CheckOrigin: func(r *http.Request) bool { return true }, } type Conn struct { Socket *websocket.Conn Id string Send chan []byte Rooms map[string]*Room } var ( ConnManager = make(map[string]*Conn) Emitter = emission.NewEmitter() ) func (c *Conn) HandleData(data []byte, msg *Message) error { switch msg.Event { case "join": c.Join(msg.Room) case "leave": c.Leave(msg.Room) default: if msg.Dst != "" { if dst, ok := c.Rooms[msg.Room].Members[msg.Dst]; ok { dst.Send <- data } } else { c.Emit(data, msg)