Skip to content

Connect to another server #96

Closed Answered by lxzan
vinaysanga asked this question in Q&A
Jun 14, 2024 · 4 comments · 4 replies
Discussion options

You must be logged in to vote
package main

import (
	"github.com/lxzan/gws"
	"net/http"
)

const (
	protocol = "ws"
	addr     = "127.0.0.1:9001"
)

func GetValueFromSession[T any](session gws.SessionStorage, key string) (value T) {
	if v, ok := session.Load(key); ok {
		value = v.(T)
	}
	return value
}

func main() {
	upgrader := gws.NewUpgrader(new(ServerHandler), &gws.ServerOption{
		PermessageDeflate: gws.PermessageDeflate{
			Enabled:               true,
			ServerContextTakeover: true,
			ClientContextTakeover: true,
		},
	})
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		sock, err := upgrader.Upgrade(w, r)
		if err != nil {
			return
		}
		url := r.URL
		url.Scheme, url.Host = protocol, addr

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@lxzan
Comment options

@vinaysanga
Comment options

@lxzan
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by lxzan
Comment options

You must be logged in to vote
1 reply
@vinaysanga
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants