Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i implement a websocket using httprouter #309

Open
dbhys4github opened this issue Apr 27, 2020 · 2 comments
Open

How can i implement a websocket using httprouter #309

dbhys4github opened this issue Apr 27, 2020 · 2 comments

Comments

@dbhys4github
Copy link

As the title:How can i implement a websocket using httprouter?

@han-so1omon
Copy link

han-so1omon commented Jun 4, 2020

I got it working with something like the code below. I'm using gorilla/websocket for websocket connection management and messaging.

//////////////////////////// server.go
package main

import (
	"github.com/gorilla/websocket"
	"github.com/julienschmidt/httprouter"
	"log"
	"net/http"
)

var upgrader = websocket.Upgrader{}

func internalError(ws *websocket.Conn, msg string, err error) {
	log.Println(msg, err)
	ws.WriteMessage(websocket.TextMessage, []byte("Internal server error."))
}

func sendStuff(ws *websocket.Conn, /* stuff */) {
	ws.WriteMessage(websocket.TextMessage, []byte(/* stuff */)
}

func Connect(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	// Setup websocket
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println("upgrade:", err)
		return
	}
        
        // Do some preprocessing here...

	defer ws.Close()

	// Do some more processing if you want...
	done := false
	for {
		select {
		case /* <-appDone */:
			done = true
		case /* <-appUpdated */:
			go sendStuff(ws, /*stuff*/)
		}
		if done {
			break
		}
	}
}
func NewRouter() *httprouter.Router {
	router := httprouter.New()
	router.GET("/ws", Connect)

	return router
}

//////////////////////////// main.go
package main

import (
	"log"
	"net/http"
)

func main() {
	log.Println("Starting app")
	// Get new router
	router := NewRouter()
	// Serve routes
	http.ListenAndServe(":8900", router)
}

Here's my client skeleton code as well in Python.

import os
import sys
import websocket

try:
    import thread
except ImportError:
    import _thread as thread

import time

def process_message(message):
    print(message)

def on_message(ws, message):
    process_message(message)

def on_error(ws, error):
    print("error")
    print(error)

def on_close(ws):
    print("### end ###")

def on_open(ws):
    def run(*args):
        while True:
            time.sleep(1)
        time.sleep(1)
        ws.close()
        print("terminating websocket...")
    thread.start_new_thread(run, ())

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(
            "ws://localhost:8900/ws",
            on_message = on_message,
            on_error = on_error,
            on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

This confused me a bit as well, as I wasn't sure which http method to use with a websocket. I settled on the GET method, as that's what I've seen in some other places. Everything else as shown, substituting POST instead of GET yields 405 Method Not Allowed.

@han-so1omon
Copy link

@julienschmidt I recommend closing this issue and labeling as question

similark pushed a commit to similarweb/httprouter that referenced this issue May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants