Esempio n. 1
0
File: hub.go Progetto: hjr265/tonesa
func HandleSocket(s *glue.Socket) {
	s.OnClose(func() {
		err := UnsubscribeAll(s)
		if err != nil {
			log.Print(err)
		}
	})

	s.OnRead(func(data string) {
		fields := strings.Fields(data)
		if len(fields) == 0 {
			return
		}
		switch fields[0] {
		case "watch":
			if len(fields) != 2 {
				return
			}
			err := Subscribe(s, fields[1])
			if err != nil {
				log.Print(err)
			}

		case "touch":
			if len(fields) != 4 {
				return
			}
			err := Emit(fields[1], "touch:"+fields[2]+","+fields[3])
			if err != nil {
				log.Print(err)
			}
		}
	})
}
Esempio n. 2
0
func (h *Hub) HandleSocket(sock *glue.Socket) {
	sock.OnClose(func() {
		h.nexus.detach(sock)
	})

	sock.OnRead(func(data string) {
		fields := strings.SplitN(data, " ", 2)
		if len(fields) != 2 {
			return
		}
		switch fields[0] {
		case "attach":
			h.attachInCh <- AttachIn{
				Socket: sock,
				Token:  fields[1],
			}

		case "change":
			data := ChangeData{}
			err := json.Unmarshal([]byte(fields[1]), &data)
			if err != nil {
				h.sendError(sock, "bad request")
				return
			}

			h.changeInCh <- ChangeIn{
				Socket: sock,
				Change: data.Change(),
			}
		}
	})
}
Esempio n. 3
0
func onNewSocket(s *glue.Socket) {
	s.OnRead(func(data string) {
		// Echo back.
		s.Write(data)
	})

	s.OnClose(func() {
		println("closed")
	})

	s.Write("Hello Client")
}
Esempio n. 4
0
File: main.go Progetto: ljgago/glue
func onNewSocket(s *glue.Socket) {
	// Set a function which is triggered as soon as the socket is closed.
	s.OnClose(func() {
		log.Printf("socket closed with remote address: %s", s.RemoteAddr())
	})

	// Set a function which is triggered during each received message.
	s.OnRead(func(data string) {
		// Echo the received data back to the client.
		s.Write(data)
	})

	// Send a welcome string to the client.
	s.Write("Hello Client")
}