For background, see this recent stackoverflow article.
I am using Golang Fiber to set up a WebSockets service like this:
app.Use("/ws", handler.UpgradeHandler)
app.Get("/ws/:id", websocket.New(func(ws *websocket.Conn) {
handler.MediaStreamHandler(ws)
}))
with UpgradeHandler
, the middleware, like so:
func UpgradeHandler(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
log.Info("request to open WebSockets channel")
return c.Next()
}
return fiber.ErrUpgradeRequired
}
The expectation is that an HTTP client to this service makes an HTTP GET request to something like http://chat.acme.ai:80/ws/123
, and that coerces the Fiber server into opening up a WebSockets service. Eg:
func MediaStreamHandler(c *websocket.Conn) {
...
for {
if mt, buf, err = c.ReadMessage(); err != nil {
...
}
...
}
where we ingest streaming media until the WebSocket is closed.
QUESTION: if the client initiates an HTTP GET request to Fiber service to stand up the WebSockets service on Fiber's side, then how does the client-side logic morph its HTTP client into a WebSockets client?
Intuitively, we just have a socket somewhere, and upgrading an HTTP client to a WebSockets client is merely tacking on the right "glue" to the HTTP client. But from the perspective of using a canned solution like, say, Java WebSockets, it's not at all clear how you would morph the HTTP client into a WebSockets client.