Closed jason-shen closed 4 years ago
Hi,
As you can see in the example, you can send data using the kws.Emit method.
If you want to send to a specific connection, you can use kws.EmitTo.
I hope that helps.
Thank you, Antonio
On Thu, Aug 27, 2020, 11:37 Jason Shen notifications@github.com wrote:
firstly this is a very good idea
i had a look through, but couldn't understand how to emit a message back to the client from server, the reading from client i understand, can you please explain it to me
thank you so much
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/antoniodipinto/ikisocket/issues/2, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIBGLOGEHPSTPKWAZAC6TLSCYLMZANCNFSM4QMXE4GA .
Hi, As you can see in the example, you can send data using the kws.Emit method. If you want to send to a specific connection, you can use kws.EmitTo. I hope that helps. Thank you, Antonio … On Thu, Aug 27, 2020, 11:37 Jason Shen @.***> wrote: firstly this is a very good idea i had a look through, but couldn't understand how to emit a message back to the client from server, the reading from client i understand, can you please explain it to me thank you so much — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#2>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIBGLOGEHPSTPKWAZAC6TLSCYLMZANCNFSM4QMXE4GA .
i through thats what it does, but it doesn't seem to fired as my client don't receive any messages from the server,
also
//// Multiple event handling supported ikisocket.On(ikisocket.EventConnect, func(ep *ikisocket.EventPayload) { fmt.Println("fired connect 1") })
//this doesn't log out the user_id ikisocket.On("message", func(payload *ikisocket.EventPayload) { fmt.Println("fired message " + payload.SocketAttributes["user_id"]) })
what is the use of these events?
cheers
Please post here the code where you're trying to emit the data.
That event handling system allows you to handle events outside the websocket route callback.
`package main
import ( "encoding/json" "fmt" ikisocket "github.com/antoniodipinto/ikisocket" "github.com/gofiber/fiber" )
// Basic chat message object
type MessageObject struct {
Data string json:"data"
From string json:"from"
To string json:"to"
}
func main() {
// The key for the map is message.to
clients := make(map[string]string)
// Start a new Fiber application
app := fiber.New()
// Setup the middleware to retrieve the data sent in first GET request
app.Use(func(c *fiber.Ctx) {
c.Locals("user_id", c.Query("user_id"))
c.Next()
})
//// Multiple event handling supported
ikisocket.On(ikisocket.EventConnect, func(ep *ikisocket.EventPayload) {
fmt.Println("fired connect 1")
})
ikisocket.On(ikisocket.EventConnect, func(ep *ikisocket.EventPayload) {
fmt.Println("fired connect 2")
})
ikisocket.On(ikisocket.EventMessage, func(ep *ikisocket.EventPayload) {
fmt.Println("fired message: " + string(ep.Data))
})
ikisocket.On(ikisocket.EventDisconnect, func(ep *ikisocket.EventPayload) {
fmt.Println("fired disconnect" + ep.Error.Error())
})
// Websocket route init
app.Get("/ws", ikisocket.New(func(kws *ikisocket.Websocket) {
// Retrieve user id from the middleware (optional)
userId := fmt.Sprintf("%v", kws.Locals("user_id"))
// fmt.Println("userID", userId)
// Every websocket connection has an optional session key => value storage
kws.SetAttribute("user_id", userId)
// On connect event. Notify when comes a new connection
kws.OnConnect = func() {
// Add the connection to the list of the connected clients
// The UUID is generated randomly
clients[userId] = kws.UUID
//Broadcast to all the connected users the newcomer
kws.Broadcast([]byte("New user connected: "+userId+" and UUID: "+kws.UUID), true)
//Write welcome message
kws.Emit([]byte("Hello user: " + userId + " and UUID: " + kws.UUID))
}
// On message event
kws.OnMessage = func(data []byte) {
message := MessageObject{}
json.Unmarshal(data, &message)
// Emit the message directly to specified user
fmt.Println("data", data)
err := kws.EmitTo(clients[message.To], data )
if err != nil {
fmt.Println(err)
}
}
}))
ikisocket.On("message", func(payload *ikisocket.EventPayload) {
fmt.Println("fired message " + payload.SocketAttributes["user_id"])
})
ikisocket.On("close", func(payload *ikisocket.EventPayload) {
fmt.Println("fired close " + payload.SocketAttributes["user_id"])
})
// Start the application on port 3000
app.Listen(3000)
}`
here is the client codes `<!DOCTYPE html>
That event handling system allows you to handle events outside the websocket route callback.
possible to send from here?
OFC it's possible, seems to be a strange issue that allow you to connect but no sending the data. I will check where the issue can be.
Thank you for posting it
Antonio
OFC it's possible, seems to be a strange issue that allow you to connect but no sending the data. I will check where the issue can be.
Thank you for posting it
Antonio
how would you emit back from outside the websocket route?
// Metacode
.On(event, ep{ ep.Kws.Emit })
// Metacode
.On(event, ep{ ep.Kws.Emit })
you mean like this?
ikisocket.On("message", ep{ ep.Kws.Emit })
@jason-shen Like this
ikisocket.On(ikisocket.EventMessage, func(ep *ikisocket.EventPayload) {
ep.Kws.Emit(<YOUR BYTE ARRAY HERE>);
})
@jason-shen Like this
ikisocket.On(ikisocket.EventMessage, func(ep *ikisocket.EventPayload) { ep.Kws.Emit(<YOUR BYTE ARRAY HERE>); })
got you
also `userId := fmt.Sprintf("%v", kws.Locals("user_id")) // fmt.Println("userID", userId) // Every websocket connection has an optional session key => value storage kws.SetAttribute("user_id", userId)
// On connect event. Notify when comes a new connection
kws.OnConnect = func() {
// Add the connection to the list of the connected clients
// The UUID is generated randomly
clients[userId] = kws.UUID
//Broadcast to all the connected users the newcomer
kws.Broadcast([]byte("New user connected: "+userId+" and UUID: "+kws.UUID), true)
//Write welcome message
kws.Emit([]byte("Hello user: " + userId + " and UUID: " + kws.UUID))
}`
is this part require to make it work?
as this is kinda a repeat of what the .On event does
cheers
The part that you copied is another way to achieving the same thing. You have OnMessage and OnConnect wrapper callbacks available for you.
as this is kinda a repeat of what the .On event does
Yes and No, it's up to you
The part that you copied is another way to achieving the same thing. You have OnMessage and OnConnect wrapper callbacks available for you.
as this is kinda a repeat of what the .On event does
Yes and No, it's up to you
Ok got u, so I can just go socket.New()
And use the .on events
Correct
Like this
app.Get("/ws", ikisocket.New())
Correct
firstly this is a very good idea
i had a look through, but couldn't understand how to emit a message back to the client from server, the reading from client i understand, can you please explain it to me
thank you so much