sipgo icon indicating copy to clipboard operation
sipgo copied to clipboard

sipgo ws integarted in webserver

Open wnke opened this issue 5 months ago • 6 comments

Hello!

Is is possible to integrate sipgo ws on a http server? In particular I am using Labstack Echo and trying to understand how to go about having /ws using SIPGO and the rest as a simple webserver.

I am asking because all transports on sipgo start their own listener and that would not fit this presuppose.

package main

import (
	"fmt"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"golang.org/x/net/websocket"
)

func hello(c echo.Context) error {
	websocket.Handler(func(ws *websocket.Conn) {
		defer ws.Close()

                // use sipgo here
		for {
			// Write
			err := websocket.Message.Send(ws, "Hello, Client!")
			if err != nil {
				c.Logger().Error(err)
			}

			// Read
			msg := ""
			err = websocket.Message.Receive(ws, &msg)
			if err != nil {
				c.Logger().Error(err)
			}
			fmt.Printf("%s\n", msg)
		}
	}).ServeHTTP(c.Response(), c.Request())
	return nil
}

func main() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Static("/", "../public")
	e.GET("/ws", hello)
	e.Logger.Fatal(e.Start(":1323"))
}

wnke avatar Sep 17 '24 09:09 wnke