Giter VIP home page Giter VIP logo

Comments (12)

welcome avatar welcome commented on May 18, 2024

Thanks for opening your first issue here! πŸŽ‰ Be sure to follow the issue template!

from recipes.

WLun001 avatar WLun001 commented on May 18, 2024

Hi @maverickVision, I think you need separate websocket for that. And your API server connects to your websocket

app.Get("/operation", func (c *fiber.Ctx) error {

ws.send('message')

return c.SendString("done!")
})

from recipes.

marcelo-tm avatar marcelo-tm commented on May 18, 2024

Hi @WLun001, I've created this simple repo with the minimal working structure using fiber and it's websockets plugin, could you help me modify it to get it working?

https://github.com/maverickVision/testws

from recipes.

WLun001 avatar WLun001 commented on May 18, 2024

@maverickVision I am not free at the moment, maybe you could take a look at this repo that address the same issue.

Checkout on problem branch.

from recipes.

marcelo-tm avatar marcelo-tm commented on May 18, 2024

Thanks @WLun001, I'll take a look. But I already saw that you are using the gin framework. Since I'm still beggining with Go, do you think it's better to learn gin instead of fiber?

from recipes.

Fenny avatar Fenny commented on May 18, 2024

I'm not sure if I fully understand the question, but are you trying to run a websocket server with TLS?

package main

import (
	"crypto/tls"
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/websocket/v2"
)

func main() {
	// Fiber instance
	app := fiber.New()

	// Websocket upgrade
	app.Get("/ws", websocket.New(func(c *websocket.Conn) {
		var (
			mt  int
			msg []byte
			err error
		)
		for {
			if mt, msg, err = c.ReadMessage(); err != nil {
				log.Println("read:", err)
				break
			}
			log.Printf("recv: %s", msg)

			if err = c.WriteMessage(mt, msg); err != nil {
				log.Println("write:", err)
				break
			}
		}
	}))

	// Load tls certificate
	cer, err := tls.LoadX509KeyPair("./certs/ssl.cert", "./certs/ssl.key")
	if err != nil {
		log.Fatal(err)
	}

	config := &tls.Config{Certificates: []tls.Certificate{cer}}

	// Create custom listener
	ln, err := tls.Listen("tcp", ":443", config)
	if err != nil {
		panic(err)
	}

	// Access the websocket server: wss://localhost:443/ws
	// https://www.websocket.org/echo.html
	log.Fatal(app.Listener(ln))
}

from recipes.

WLun001 avatar WLun001 commented on May 18, 2024

Thanks @WLun001, I'll take a look. But I already saw that you are using the gin framework. Since I'm still beggining with Go, do you think it's better to learn gin instead of fiber?

@maverickVision If you coming from Express, Fiber would be good choice, it has familiar APIs. Any framework will do, or without framework. That's my personal opinion.

from recipes.

marcelo-tm avatar marcelo-tm commented on May 18, 2024

@maverickVision I am not free at the moment, maybe you could take a look at this repo that address the same issue.

Checkout on problem branch.

@WLun001 I'm following your code from this repo, on the solution brach. The problem with fiber is that I can't access the responseWriter from fiber Context, in order to upgrade the connection inside the ServeWs method... and according to this issue #721, it is really not possible. I guess I will need to rewrite my project with Gin just because of this issue.

from recipes.

WLun001 avatar WLun001 commented on May 18, 2024

@maverickVision You don't need to access to responseWriter. You could use https://github.com/gofiber/websocket.

from recipes.

marcelo-tm avatar marcelo-tm commented on May 18, 2024

@maverickVision You don't need to access to responseWriter. You could use https://github.com/gofiber/websocket.

I've made the following changes:

main.go

app.Get("/ws", websocket.New(func(c *websocket.Conn) {
		ws.ServeWs(hub, c, nc, subject)
	}))

ws.go

func ServeWs(hub *Hub, conn *websocket.Conn, nc *nats.Conn, subject string) {
	client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}

	// subscribe nats
	sub, err := nc.Subscribe(subject, func(m *nats.Msg) {
		log.Println(string(m.Data))
		client.hub.broadcast <- m.Data
	})
	if err != nil {
		log.Fatal(err)
	}
	nc.Flush()

	if err := nc.LastError(); err != nil {
		log.Fatal(err)
	}

	client.natsSub = sub
	client.hub.register <- client

	// Allow Connection of memory referenced by the calloer by doing all work in new goroutines
	go client.writePump()
	go client.readPump()
}

I've switched the conn variable from the upgraded gin context to fiber websocket conn. All services gone up without issues (using the same commands you use in your code), but when I open a browser tab on localhost:8080, I get the following error on the make start-api console log:

2020/10/12 13:53:44 ws.go:64: Current connected ws client: 1
2020/10/12 13:53:44 ws.go:71: Unsubscribe
2020/10/12 13:53:44 ws.go:74: Current connected ws client: 0
panic: runtime error: invalid memory address or nil pointer dereference
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x149e03e]

goroutine 8 [running]:
github.com/fasthttp/websocket.(*Conn).Close(...)
	/Users/marcelo/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:344
github.com/maverickvision/testws/internal/ws.(*Client).writePump.func1(0xc00007e320, 0xc00028a0a0)
	/Users/marcelo/playground/golang/testws/internal/ws/ws.go:160 +0x3e
panic(0x150bc00, 0x1a06520)
	/usr/local/go/src/runtime/panic.go:969 +0x166
github.com/fasthttp/websocket.(*Conn).SetWriteDeadline(...)
	/Users/marcelo/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:781
github.com/maverickvision/testws/internal/ws.(*Client).writePump(0xc00028a0a0)
	/Users/marcelo/playground/golang/testws/internal/ws/ws.go:166 +0x311
created by github.com/maverickvision/testws/internal/ws.ServeWs
	/Users/marcelo/playground/golang/testws/internal/ws/ws.go:221 +0x240

I printed the conn and client variables on ws.go, and they are not null, what could it be?

I've updated my repo with the latest code, if you can help.

from recipes.

WLun001 avatar WLun001 commented on May 18, 2024

@maverickVision I created a simplified example,using websocket in Fiber. You may take a look at this repo

from recipes.

Fenny avatar Fenny commented on May 18, 2024

Closing due to inactivity, feel free to re-open.

from recipes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.