コード例 #1
0
ファイル: hub.go プロジェクト: sech90/go-message-hub
func (hub *Hub) processRequest(socket *mexsocket.MexSocket, req *message.Request) {

	if req == nil {
		return
	}

	switch req.MexType {

	//create a new answer and send it over the channel
	case message.Identity:
		answer := message.NewAnswerIdentity(socket.Id)
		socket.Send(answer)

	//get the list of connected clients, remove the current one and send it over the channel
	case message.List:

		//the resulting list is []interface{}
		list := set.Difference(hub.idSet, set.New(socket.Id)).List()

		//create new answer
		answer := new(message.Answer)
		answer.MexType = message.List
		answer.Payload = convertSetList(list)
		socket.Send(answer)

	case message.Relay:

		//create an answer containing the payload
		answer := message.NewAnswerRelay(req.Body)

		//call hub to send the message to the list of clients provided
		hub.Multicast(req.Receivers, answer)
	}
}
コード例 #2
0
ファイル: client.go プロジェクト: sech90/go-message-hub
func (c *Client) queueAnswer(ans *message.Answer) {

	var ch chan *message.Answer

	switch ans.MexType {
	case message.Identity:
		c.id = ans.Id()
		ch = c.incomingId
	case message.List:
		c.lastClientList = ans.List()
		ch = c.incomingList
	case message.Relay:
		ch = c.incomingRelay
	default:
		log.Println("Client", c.Id(), "Received unknown answer", ans.MexType)
		return
	}

	//Exit if client closes
	select {
	case ch <- ans:
		return
	case <-c.quitting:
		return
	}
}
コード例 #3
0
ファイル: hub.go プロジェクト: sech90/go-message-hub
//Broadcast the message to the clients with id contained in the list
func (hub *Hub) Multicast(ids []uint64, mex *message.Answer) {

	//convert the message once
	bytes := mex.ToByteArray()
	for _, id := range ids {

		//get client from map
		s, ok := hub.socketMap.Get(id)

		if ok == true {
			//send the data directly to client's write goroutine
			s.(*mexsocket.MexSocket).OutgoingBinary() <- bytes
		}
	}

}
コード例 #4
0
ファイル: message_test.go プロジェクト: sech90/go-message-hub
func TestAnswerConversion(t *testing.T) {

	a1 := message.NewAnswerIdentity(testId)
	a2 := message.NewAnswerList(testList)
	a3 := message.NewAnswerRelay(testBody)

	b1 := a1.ToByteArray()
	b2 := a2.ToByteArray()
	b3 := a3.ToByteArray()

	c1 := new(message.Answer)
	c2 := new(message.Answer)
	c3 := new(message.Answer)
	c4 := new(message.Answer)

	e1 := c1.FromByteArray(b1)
	e2 := c2.FromByteArray(b2)
	e3 := c3.FromByteArray(b3)
	e4 := c4.FromByteArray(nil)

	assert.Nil(t, e1, "No error in conversion (1)")
	assert.Nil(t, e2, "No error in conversion (2)")
	assert.Nil(t, e3, "No error in conversion (3)")
	assert.NotNil(t, e4, "Conversion from nil throws an error (4)")

	assert.Nil(t, testutils.CompareAnswer(a1, c1), "Identity answer conversion wrong")
	assert.Nil(t, testutils.CompareAnswer(a2, c2), "List answer conversion wrong")
	assert.Nil(t, testutils.CompareAnswer(a3, c3), "Relay answer conversion wrong")

}