Skip to content

Commit

Permalink
Change ack from pointer func to func
Browse files Browse the repository at this point in the history
  • Loading branch information
doquangtan committed Dec 22, 2024
1 parent 58328b5 commit d202e5c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func socketIoRoute(app fiber.Router) {
socket.On("chat message", func(event *socketio.EventPayload) {
socket.Emit("chat message", event.Data[0])

if event.Callback != nil {
(*event.Callback)("hello", map[string]interface{}{
if event.Ack != nil {
event.Ack("hello", map[string]interface{}{
"Test": "ok",
})
}
Expand Down
7 changes: 6 additions & 1 deletion example/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value, function (status, obj) {
socket.emit('chat message', input.value + " with ack", function (status, obj) {
console.log(status, obj);
var item = document.createElement('li');
item.textContent = status;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
socket.emit('chat message', input.value);
input.value = '';
}
});
Expand Down
14 changes: 7 additions & 7 deletions listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package socketio

import "sync"

type Callback func(data ...interface{})
type AckCallback func(data ...interface{})

type EventPayload struct {
Name string //event name
SID string //socket id
Socket *Socket
Error error
Data []interface{}
Callback *Callback
Name string //event name
SID string //socket id
Socket *Socket
Error error
Data []interface{}
Ack AckCallback
}

type eventCallback func(data *EventPayload)
Expand Down
2 changes: 1 addition & 1 deletion make.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ IF %func%==public (
echo Done
)

@REM make public v0.1.6
@REM make public v0.1.7
38 changes: 19 additions & 19 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type payload struct {
socket *Socket
data interface{}
ack string
ackId string
}

type Io struct {
Expand Down Expand Up @@ -124,27 +124,27 @@ func (s *Io) read(ctx context.Context) {
for _, callback := range payLoad.socket.listeners.get(event) {
data := append([]interface{}{}, dataJson[1:]...)

ackCallback := Callback(func(data ...interface{}) {
payLoad.socket.ack(payLoad.ack, data...)
ackCallback := AckCallback(func(data ...interface{}) {
payLoad.socket.ack(payLoad.ackId, data...)
})

if payLoad.ack == "" {
if payLoad.ackId == "" {
callback(&EventPayload{
SID: payLoad.socket.Id,
Name: event,
Socket: payLoad.socket,
Error: nil,
Data: data,
Callback: nil,
SID: payLoad.socket.Id,
Name: event,
Socket: payLoad.socket,
Error: nil,
Data: data,
Ack: nil,
})
} else {
callback(&EventPayload{
SID: payLoad.socket.Id,
Name: event,
Socket: payLoad.socket,
Error: nil,
Data: data,
Callback: &ackCallback,
SID: payLoad.socket.Id,
Name: event,
Socket: payLoad.socket,
Error: nil,
Data: data,
Ack: ackCallback,
})
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (s *Io) new() func(ctx *fiber.Ctx) error {
startNamespace := strings.Index(rawpayload, "/")
endNamespace := -1
startPayload := -1
ackEvent := ""
ackId := ""
if startNamespace == 0 {
special1 := strings.Index(mess, "{")
special2 := strings.Index(mess, "[")
Expand Down Expand Up @@ -260,7 +260,7 @@ func (s *Io) new() func(ctx *fiber.Ctx) error {
}

if special3 != -1 && special2 != -1 && (special2-1 != special3) {
ackEvent = string(message[special3+1 : special2])
ackId = string(message[special3+1 : special2])
}
}

Expand Down Expand Up @@ -358,7 +358,7 @@ func (s *Io) new() func(ctx *fiber.Ctx) error {
s.readChan <- payload{
socket: socket_nps,
data: rawpayload,
ack: ackEvent,
ackId: ackId,
}
}
}
Expand Down

0 comments on commit d202e5c

Please sign in to comment.