Skip to content

Commit

Permalink
Adds echo implementation of WS server
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 27, 2024
1 parent 470aa44 commit 90d3872
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"
"log/slog"
_ "net/http/pprof"
"os"

"github.com/ksysoev/wasabi"
"github.com/ksysoev/wasabi/channel"
"github.com/ksysoev/wasabi/dispatch"
"github.com/ksysoev/wasabi/server"
)

const (
Addr = ":8080"
)

func main() {

slog.LogAttrs(context.Background(), slog.LevelDebug, "")

backend := dispatch.RequestHandlerFunc(func(conn wasabi.Connection, req wasabi.Request) error {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

dispatcher := dispatch.NewPipeDispatcher(backend)
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Addr, server.WithBaseContext(context.Background()))
server.AddChannel(channel)

if err := server.Run(); err != nil {
slog.Error("Fail to start app server", "error", err)
os.Exit(1)
}

fmt.Println("Server is stopped")
os.Exit(0)
}

0 comments on commit 90d3872

Please sign in to comment.