Updated nats examples

This commit is contained in:
Xackery 2018-03-16 18:43:14 -07:00
parent a5f35bf32a
commit 825dbc9e9a
3 changed files with 32 additions and 6 deletions

View File

@ -28,14 +28,14 @@ func main() {
//send a channel message to broadcast channel
go testBroadcastMessage(nc, "Hello, World!")
time.Sleep(20 * time.Second)
fmt.Println("Exited after 20 seconds")
time.Sleep(100 * time.Second)
fmt.Println("Exited after 100 seconds")
}
// asyncChannelMessageSubscriber is an example of how to subscribe
// and invoke a function when a message is received
func asyncChannelMessageSubscriber(nc *nats.Conn) {
nc.Subscribe("world.channel_message", func(m *nats.Msg) {
nc.Subscribe("world.channel_message.out", func(m *nats.Msg) {
message := &eqproto.ChannelMessage{}
proto.Unmarshal(m.Data, message)
log.Println(message)
@ -66,7 +66,7 @@ func testBroadcastMessage(nc *nats.Conn, msg string) {
message := &eqproto.ChannelMessage{
From: "go",
Message: msg,
ChanNum: 5, //5 is ooc, 6 is bc
Number: 5, //5 is ooc, 6 is bc
}
d, err := proto.Marshal(message)
if err != nil {

View File

@ -30,6 +30,7 @@ func main() {
entities = zoneEntityList(zone, 0)
fmt.Println(len(entities), "entities known")
//fmt.Println(entities)
var entityID int32
for _, entity := range entities {
@ -42,7 +43,7 @@ func main() {
if entityID == 0 {
log.Fatal("Can't find entity!")
}
go asyncChannelMessageSubscriber(nc) //async is recommended
go entityEventSubscriber(zone, instance, entityID)
zoneChannel(zone, instance, entityID, eqproto.EntityType_Client, eqproto.MessageType_Say, "Hello, World!")
time.Sleep(1000 * time.Second)
@ -116,6 +117,12 @@ func zoneEntityList(zone string, instanceID int) (entities []*eqproto.Entity) {
return
}
if msg.Result != "1" {
fmt.Println("Failed response: ", msg.Result)
return
}
//fmt.Println("reply", len(msg.Payload), string(msg.Payload))
rootEntities := &eqproto.Entities{}
err = proto.Unmarshal([]byte(msg.Payload), rootEntities)
if err != nil {
@ -198,6 +205,7 @@ func entityEventSubscriber(zone string, instance int64, entityID int32) {
}*/
var index int
channel := fmt.Sprintf("zone.%s.%d.entity.%d.event.out", zone, instance, entityID)
nc.Subscribe(channel, func(m *nats.Msg) {
event := &eqproto.Event{}
@ -226,6 +234,8 @@ func entityEventSubscriber(zone string, instance int64, entityID int32) {
eventPayload = &eqproto.DamageEvent{}
case eqproto.OpCode_OP_SpecialMesg:
eventPayload = &eqproto.SpecialMessageEvent{}
case eqproto.OpCode_OP_ChannelMessage:
eventPayload = &eqproto.ChannelMessageEvent{}
default:
return
}
@ -244,3 +254,19 @@ func entityEventSubscriber(zone string, instance int64, entityID int32) {
time.Sleep(500 * time.Second)
}
// asyncChannelMessageSubscriber is an example of how to subscribe
// and invoke a function when a message is received
func asyncChannelMessageSubscriber(nc *nats.Conn) {
nc.Subscribe("world.channel_message.out", func(m *nats.Msg) {
message := &eqproto.ChannelMessage{}
proto.Unmarshal(m.Data, message)
log.Println(message)
})
nc.Subscribe("zone.ecommons.0.channel_message.out", func(m *nats.Msg) {
message := &eqproto.ChannelMessage{}
proto.Unmarshal(m.Data, message)
log.Println(message)
})
log.Println("Waiting on async messages...")
}

View File

@ -168,7 +168,7 @@ void NatsManager::SendChannelMessage(eqproto::ChannelMessage* message, const cha
if (s != NATS_OK) {
Log(Logs::General, Logs::NATS, "world.channel_message.out failed: %s", nats_GetLastError(&s));
Log(Logs::General, Logs::NATS, StringFormat("zone.%s.%d.channel_message.out failed: %s", subscribedZoneName.c_str(), subscribedZoneInstance).c_str(), nats_GetLastError(&s));
return;
}