http.Handle("/", websocket.Handler(EchoServer) Can EchoServer Get another parameter other than ws?

Farah :

I have opened a websocket server to send data to web component,

func WebSocketServer() {
    http.Handle("/", websocket.Handler(Echoserver))
    err := http.ListenAndServe(":8081", nil)
    CheckError(err)
}

I would like to pass an additionnal argument (msg, of type String) to the handlerfunction (Echoserver).

func Echoserver(ws *websocket.Conn, msg String) {
    fmt.Println("Client Connected")
         _ := websocket.JSON.Send(ws, msg);
    }
}

Is it possible to do this with the syntax above? How do you call Echoserver with the additionnal parameter ?

Rob Napier :

I assume what you want here is a consistent string parameter that is returned for all connections to /. There are a couple of approaches I've used. (None of these specific code samples have been tested; I can pull some real code if they don't compile.)

One is to let the data be the receiver. I use this a most often with a struct, but any parameter will do. This only works for a single parameter (but you could of course put multiple parameters in a struct). I like this approach when the parameter is "object like." (Generally a struct that has other methods on it.)

type echoStuff string

var hey echoStuff = "Hey!"

http.Handle("/", websocket.Handler(hey.EchoServer))
err := http.ListenAndServe(":8081", nil)
CheckError(err)

func (msg echoStuff) Echoserver(ws *websocket.Conn) {
  fmt.Println("Client Connected")
   _ := websocket.JSON.Send(ws, msg);
}

Another way is with a closure. I like this approach when the parameter is "data like." (Something like a string or other simple data. Note how this approach doesn't require creating a local type.)

func WebSocketServer() {
  http.Handle("/", echoHandler("Hey!"))
  err := http.ListenAndServe(":8081", nil)
  CheckError(err)
}

func echoHandler(msg string) websocket.Handler {
  return func(ws *Conn) {
    Echoserver(ws, msg)
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can subscribe to ws websocket from a nodejs server to another server?

Django can only handle ASGI/HTTP connections, not websocket

How to run a websocket server on ws and wss at same time that they both communicate or sync data with each other? Or WSS on HTTP and WS on HTTPS?

How can I get a startup application to run in another workspace other than #1?

In omnet++ can handleMessage handle any class other than cMessage?

How does Nginx Ingress handle an incoming WebSocket Connection over HTTPS (wss:...) when routing to backend over HTTP? (ws:...)?

how to get the cookies of two servers 1. runs application (HTTP) , 2. runs websocket(WS) server?

Can I use another port other than 443 for SSL communication?

Can a XML namespace name be something other than an HTTP URI?

Keycloak says 403 Forbidden for HTTP Methods other than GET

Getting a "Django can only handle ASGI/HTTP connections, not websocket." error when hosting ASGI on heroku?

Handle WebSocket error with http-proxy-middleware

Akka-http approach to handle websocket commands

How can I get data other than 24 Hours in Mongoose

How can i get host application handle in dll project with Delphi without passing handle parameter

How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

Can an http server and a websocket server running on the same NodeJS process communicate with each other locally?

Why can't type parameter in Kotlin have any other bounds if it's bounded by another type parameter?

Should WebSocket server only handle GET requests?

Sending objects through websocket using ws: Can't deserialize

can't create a websocket sever correctly by express-ws middleware

Can I change WebSocket from ws to wss? (000webhost)

Can I customize the `resources` helper in my routes to use a parameter other than \:id?

Python3: Can use other than 'self' as a 1st parameter of methods?

Redirect HTTP upgrade to a websocket on another instance

Angular, Http GET with parameter?

React Admin: Can I use another field for <ReferenceInput>, other than "id"?

Proxied requests other than GET are hanging with NestJS and http-proxy-middleware

What happens if a Http.get request in Elm receives a different type of response other than expected one?