Exemplo n.º 1
0
// HandleIncoming responds to messages that equal "!ping" or "!ping @MaiMai"
// with a "pong!" message over the Connection.
func (h *PongHandler) HandleIncoming(conn mproto.Connection, p *proto.Packet) error {
	if p.Type != proto.SendEventType {
		return nil
	}
	payload, err := p.Payload()
	if err != nil {
		return err
	}
	sendEvent, ok := payload.(*proto.SendEvent)
	if !ok {
		return fmt.Errorf("Error asserting send-event as such.")
	}
	if sendEvent.Content != "!ping" && sendEvent.Content != "!ping @MaiMai" {
		return nil
	}
	reply := &proto.SendCommand{
		Content: "pong!",
		Parent:  sendEvent.ID,
	}
	marshal, err := json.Marshal(reply)
	if err != nil {
		return err
	}
	return conn.Send(&proto.Packet{
		Type: proto.SendType,
		Data: marshal,
	})
}
Exemplo n.º 2
0
// HandleIncoming responds to the given ping-event over the given Connection.
func (h *PingHandler) HandleIncoming(conn mproto.Connection, p *proto.Packet) error {
	if p.Type != proto.PingEventType {
		return nil
	}
	payload, err := p.Payload()
	if err != nil {
		return err
	}
	pingEvent, ok := payload.(*proto.PingEvent)
	if !ok {
		return fmt.Errorf("Error asserting ping-event as such")
	}
	pingReply := &proto.PingReply{
		UnixTime: pingEvent.UnixTime,
	}
	marshalled, err := json.Marshal(pingReply)
	if err != nil {
		return err
	}
	return conn.Send(&proto.Packet{
		Type: proto.PingReplyType,
		Data: marshalled,
	})
}