-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement progressive call invocations (#57)
* Update wampproto to latest * Implement progressive call invocations * Add function for progressive call results with progresssive call invocations * Add funcion to export session id * Add example for progressive call invocations * Add example for progressive results
- Loading branch information
1 parent
a919320
commit cd5d012
Showing
9 changed files
with
496 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/xconnio/wampproto-go" | ||
"github.com/xconnio/xconn-go" | ||
) | ||
|
||
const procedureProgressUpload = "io.xconn.progress.upload" | ||
|
||
func main() { | ||
ctx := context.Background() | ||
client := xconn.Client{} | ||
callee, err := client.Connect(ctx, "ws://localhost:8080/ws", "realm1") | ||
if err != nil { | ||
log.Fatalf("Failed to connect to server: %s", err) | ||
} | ||
defer func() { _ = callee.Leave() }() | ||
|
||
invocationHandler := func(ctx context.Context, invocation *xconn.Invocation) *xconn.Result { | ||
isProgress, _ := invocation.Details[wampproto.OptionProgress].(bool) | ||
|
||
// Handle the progressive chunk | ||
if isProgress { | ||
chunkIndex := invocation.Arguments[0].(float64) | ||
fmt.Printf("Received chunk %v\n", chunkIndex) | ||
return &xconn.Result{Err: xconn.ErrNoResult} | ||
} | ||
|
||
// Final response after all chunks are received | ||
fmt.Println("All chunks received, processing complete.") | ||
return &xconn.Result{Arguments: []any{"Upload complete"}} | ||
} | ||
|
||
registration, err := callee.Register(procedureProgressUpload, invocationHandler, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to register procedure: %s", err) | ||
} | ||
defer func() { _ = callee.Unregister(registration.ID) }() | ||
|
||
// Wait for interrupt signal to gracefully shut down | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt) | ||
select { | ||
case <-sigChan: | ||
log.Println("Interrupt signal received, shutting down.") | ||
case <-ctx.Done(): | ||
log.Println("Context canceled, shutting down.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/xconnio/wampproto-go" | ||
"github.com/xconnio/xconn-go" | ||
) | ||
|
||
const procedureProgressUpload = "io.xconn.progress.upload" | ||
|
||
func main() { | ||
ctx := context.Background() | ||
client := xconn.Client{} | ||
caller, err := client.Connect(ctx, "ws://localhost:8080/ws", "realm1") | ||
if err != nil { | ||
log.Fatalf("Failed to connect to server: %s", err) | ||
} | ||
defer func() { _ = caller.Leave() }() | ||
|
||
totalChunks := 6 | ||
chunkIndex := 0 | ||
|
||
// Simulate file data being uploaded in chunks | ||
fmt.Println("Starting file upload...") | ||
|
||
result, err := caller.CallProgressive(ctx, procedureProgressUpload, func(ctx context.Context) *xconn.Progress { | ||
options := map[string]any{} | ||
|
||
// Mark the last chunk as non-progressive | ||
if chunkIndex == totalChunks-1 { | ||
options[wampproto.OptionProgress] = false | ||
} else { | ||
options[wampproto.OptionProgress] = true | ||
} | ||
|
||
// Simulate sending each chunk of the file | ||
fmt.Printf("Uploading chunk %d...\n", chunkIndex) | ||
args := []any{chunkIndex} | ||
chunkIndex++ | ||
|
||
// Simulate network delay between chunks | ||
time.Sleep(500 * time.Millisecond) | ||
|
||
return &xconn.Progress{Arguments: args, Options: options} | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to upload data: %s", err) | ||
} | ||
|
||
fmt.Println("Final result:", result.Arguments[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/xconnio/wampproto-go" | ||
"github.com/xconnio/xconn-go" | ||
) | ||
|
||
const procedureProgressUpload = "io.xconn.progress.upload" | ||
|
||
func main() { | ||
ctx := context.Background() | ||
client := xconn.Client{} | ||
callee, err := client.Connect(ctx, "ws://localhost:8080/ws", "realm1") | ||
if err != nil { | ||
log.Fatalf("Failed to connect to server: %s", err) | ||
} | ||
defer func() { _ = callee.Leave() }() | ||
|
||
invocationHandler := func(ctx context.Context, invocation *xconn.Invocation) *xconn.Result { | ||
isProgress, _ := invocation.Details[wampproto.OptionProgress].(bool) | ||
chunkIndex := invocation.Arguments[0].(float64) | ||
|
||
if isProgress { | ||
// Mirror back the received chunk as progress | ||
fmt.Printf("Received chunk %v, sending progress back\n", chunkIndex) | ||
if err = invocation.SendProgress([]any{chunkIndex}, nil); err != nil { | ||
return &xconn.Result{Err: "wamp.error.canceled", Arguments: []any{err.Error()}} | ||
} | ||
|
||
return &xconn.Result{Err: xconn.ErrNoResult} | ||
} | ||
|
||
// Final response when all chunks are received | ||
fmt.Println("All chunks received, processing complete.") | ||
return &xconn.Result{Arguments: []any{fmt.Sprintf("Upload complete, chunk %v acknowledged", chunkIndex)}} | ||
} | ||
|
||
registration, err := callee.Register(procedureProgressUpload, invocationHandler, nil) | ||
if err != nil { | ||
log.Fatalf("Failed to register method: %s", err) | ||
} | ||
defer func() { _ = callee.Unregister(registration.ID) }() | ||
|
||
// Wait for interrupt signal to gracefully shutdown | ||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, os.Interrupt) | ||
select { | ||
case <-sigChan: | ||
log.Println("Interrupt signal received, shutting down.") | ||
case <-ctx.Done(): | ||
log.Println("Context canceled, shutting down.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/xconnio/wampproto-go" | ||
"github.com/xconnio/xconn-go" | ||
) | ||
|
||
const procedureProgressUpload = "io.xconn.progress.upload" | ||
|
||
func main() { | ||
ctx := context.Background() | ||
client := xconn.Client{} | ||
caller, err := client.Connect(ctx, "ws://localhost:8080/ws", "realm1") | ||
if err != nil { | ||
log.Fatalf("Failed to connect to server: %s", err) | ||
} | ||
defer func() { _ = caller.Leave() }() | ||
|
||
totalChunks := 5 | ||
chunkIndex := 0 | ||
|
||
fmt.Println("Starting file upload...") | ||
|
||
result, err := caller.CallProgressiveProgress(ctx, procedureProgressUpload, func(ctx context.Context) *xconn.Progress { | ||
options := map[string]any{} | ||
|
||
// Mark the last chunk as non-progressive | ||
if chunkIndex == totalChunks-1 { | ||
options[wampproto.OptionProgress] = false | ||
} else { | ||
options[wampproto.OptionProgress] = true | ||
} | ||
|
||
// Simulate uploading chunk | ||
fmt.Printf("Sending chunk %d\n", chunkIndex) | ||
args := []any{chunkIndex} | ||
chunkIndex++ | ||
|
||
// Simulate delay for each chunk | ||
time.Sleep(500 * time.Millisecond) | ||
|
||
return &xconn.Progress{Arguments: args, Options: options} | ||
}, func(result *xconn.Result) { | ||
// Handle progress updates mirrored by the callee | ||
chunkProgress := result.Arguments[0].(float64) | ||
fmt.Printf("Progress update: chunk %v acknowledged by server\n", chunkProgress) | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalf("Failed to upload data: %s", err) | ||
} | ||
|
||
fmt.Printf("Upload complete: %s\n", result.Arguments[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.