Exemplo n.º 1
0
func (s Server) GetClient(request protocol.Message, conn protocol.Connection) *protocol.Client {
	clientId := request.ClientId()
	client := s.engine.GetClient(clientId)
	if client == nil {
		s.Logger().Warnf("Message %v from unknown client %v", request.Channel(), clientId)
		response := request
		response["successful"] = false
		response["advice"] = map[string]interface{}{"reconnect": "handshake", "interval": "1000"}
		conn.Send([]protocol.Message{response})
		conn.Close()
	}
	return client
}
Exemplo n.º 2
0
func (m Engine) Publish(request protocol.Message) {
	requestingClient := m.clients.GetClient(request.ClientId())

	if requestingClient == nil {
		m.logger.Warnf("PUBLISH from unknown client %s", request)
	} else {
		response := m.responseFromRequest(request)
		response["successful"] = true
		data := request["data"]
		channel := request.Channel()

		m.clients.GetClient(request.ClientId()).Queue(response)

		go func() {
			// Prepare msg to send to subscribers
			msg := protocol.Message{}
			msg["channel"] = channel.Name()
			msg["data"] = data
			// TODO: Missing ID

			msg.SetClientId(request.ClientId())

			// Get clients with subscriptions
			recipients := m.clients.GetClients(channel.Expand())
			m.logger.Debugf("PUBLISH from %s on %s to %d recipients", request.ClientId(), channel, len(recipients))
			// Queue messages
			for _, c := range recipients {
				m.clients.GetClient(c).Queue(msg)
			}
		}()

	}

}
Exemplo n.º 3
0
func (m Engine) Disconnect(request protocol.Message, client *protocol.Client, conn protocol.Connection) {
	response := m.responseFromRequest(request)
	response["successful"] = true
	clientId := request.ClientId()
	m.logger.Debugf("Client %s disconnected", clientId)
}