Problem is simple: i have WS server that doing it's "for {}". I need "register" connected user, then send message on event(outside of CoreWS function). I'm added NodeJS example that doing exactly what i need, for better understanding.
Go Lang Code that fails:
i'm using this pkg https://github.com/gobwas/ws
var conn_itf interface{} //yeah,
var op_itf interface{} //i know it's not how it works
func main() {
go collector()
go sender()
http.ListenAndServe(":3333", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
// handle error
}
go func() {
defer conn.Close()
for {
msg, op, err := wsutil.ReadClientData(conn)
conn_itf = conn // i'm trying to "register" user
op_itf = op
if err != nil {
}
err = wsutil.WriteServerMessage(conn, op, msg)
if err != nil {
}
}
}()
}))
}
func collector() {
for {
fmt.Println("conn: ", conn_itf)
fmt.Println("op: ", op_itf)
time.Sleep(10000 * time.Millisecond)
}
}
func sender() {
for {
msg := []byte("Hello world!")
time.Sleep(50000 * time.Millisecond)
wsutil.WriteServerMessage(conn_itf, op_itf, msg) //invoking ws-send
}
}
NodeJS that working:
const wss = new WebSocket.Server({ port: 3333 })
wss.on('connection', ws => {
sockets.set('key', ws) //this code remember active connection
ws.on('message', message => {
console.log(`Received message => ${message}`)
})
})
function callsend(data) {
const ws = sockets.get('key') //this code sending message on invoking
ws.send(data)
}
Related
I’m trying to build a two factor authentication in golang by gorilla/websocket
step1:
enter username and password
step2:
when server has checked the user and passed first auth, server would send email or sms to client and browser would redirect to a page let client enter the code
I watched tutorial on the web, the most common tutorial is like
// /handler/websocket.go
var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func WsPing(ctx *gin.Context) {
ws, err := upGrader.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {
return
}
defer ws.Close()
for {
mt, message, err := ws.ReadMessage()
fmt.Println(string(message))
if err != nil {
break
}
if string(message) == "ping" {
message = []byte("pong")
} else {
ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintln("got it : "+string(message))))
}
err = ws.WriteMessage(mt, message)
if err != nil {
break
}
}
}
// /router/router.go
var (
mongoClient = db.StartMongoDB()
userRepository repository.UserRepository = repository.NewUserRepository(mongoClient)
userService service.UserService = service.NewUserService(userRepository)
userHandler handler.UserHandler = handler.NewUserHandler(userService)
)
func StartGinRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", handler.WsPing)
userRoute := r.Group("/user")
{
userRoute.POST("/login", userHandler.OneAuth)
}
return r
}
But the feature I need is that when userHandler.OneAuth was implemented then socket would send a signal to frontend let the page redirect automatically.
According to the code above, I couldn't create a connection pool and import it into userHandler.OneAuth when the handler was triggered and implement the code below after server side sent message to client
err = ws.WriteMessage(mt, message)
if err != nil {
break
}
How can I achieve this goal?
I need help with Golang websocket. I'm using Fiber with websocket and redis.
Here is the code:
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"log"
"test4/controllers"
)
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
var ctx = context.Background()
var redisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
func TestSocket() fiber.Handler {
socket := websocket.New(func(c *websocket.Conn) {
go deliverMessages(c)
var (
msg []byte
err error
)
for {
if _, msg, err = c.ReadMessage(); err != nil {
log.Println("read:", err)
break
}
if err := redisClient.Publish(ctx, "chat", msg).Err(); err != nil {
log.Println("publish:", err)
break
}
}
})
return socket
}
func deliverMessages(c *websocket.Conn) {
subscriber := redisClient.Subscribe(ctx, "chat")
user := User{}
for {
msg, err := subscriber.ReceiveMessage(ctx)
if err != nil {
log.Println("subscriber:", err)
panic(err)
}
if err := json.Unmarshal([]byte(msg.Payload), &user); err != nil {
log.Println("Unmarshal:", err)
panic(err)
}
text := []byte(fmt.Sprintf("{\"name\":\"%s\", \"email\":\"%s\"}", user.Name, user.Email))
if err = c.WriteMessage(websocket.TextMessage, text); err != nil {
log.Println("write:", err)
break
}
}
}
func main() {
app := fiber.New(fiber.Config{
Prefork: true,
CaseSensitive: true,
StrictRouting: true,
DisableStartupMessage: true,
ServerHeader: "Test v3",
})
app.Get("/", controllers.Home)
app.Get("/ws", TestSocket())
log.Fatal(app.Listen("0.0.0.0:3000"))
}
How to produce the error:
Install Redis and run go run main.go
Now open http://127.0.0.1:3000/ in two tabs
click open on both tabs, and then you will see OPEN on right side of browser
click send on both tabs and you will get SEND and RESPONSE
Now close one tab and on go program terminal you will see error (see attached screenshot)
Now publish data to chat channel on redis-cli
Here is the error I am getting:
I think this is nil pointer websocket.Conn issue.
When close websocket connection, goroutine's c *websocket.Conn is loose data.
Pointer point nil.
solution
use channel, use local redisClient var
func TestSocket() fiber.Handler {
socket := websocket.New(func(c *websocket.Conn) {
var redisClient = redis.NewClient(&redis.Options{ // <-- use local redisClient var
Addr: "localhost:6379",
})
go deliverMessages(c)
var (
msg []byte
err error
)
defer func() {
redisClient.Close() // <-- then close, when websocket connection close
quitSubscribeGoRutine <- true // <-- change true, when websocket connection close
}()
for {
if _, msg, err = c.ReadMessage(); err != nil {
log.Println("read:", err)
...
func deliverMessages(c *websocket.Conn) {
subscriber := redisClient.Subscribe(ctx, "chat")
user := User{}
for {
select {
case <-quitSubscribeGoRutine: // <-- exit goroutine, when channel is true
return
default:
msg, err := subscriber.ReceiveMessage(ctx) // <-- exit goroutine, when redisClient close
if err != nil {
log.Println("subscriber:", err)
break // <-- use break instead of panic
}
if err := json.Unmarshal([]byte(msg.Payload), &user); err != nil {
log.Println("Unmarshal:", err)
panic(err)
}
text := []byte(fmt.Sprintf("{\"name\":\"%s\", \"email\":\"%s\"}", user.Name, user.Email))
if err = c.WriteMessage(websocket.TextMessage, text); err != nil {
log.Println("write:", err)
break
}
}
}
}
I want to control a listener depending on the state of something, let's say my program will listen if the content of a file is 1 and don't listen if the content is 0.
The problem is that there's not way, once a listener is initialized, to tell him to refuse connection. There is only the 'accept' state ; I have to close the listener.
So I found a way to do it using the following code, but I feel that's not a good way to do because i'm using a global and usually it's not a good idea.
Is there a better way to do it ?
var healthStatus bool
func listen(l net.Listener, port int) {
var err error
l, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
panic(err)
}
defer l.Close()
for {
if healthStatus == false {
_ = l.Close()
return
}
logrus.Debug("Going to listen !")
conn, err := l.Accept()
if err != nil {
panic(err)
}
go func(c net.Conn) {
_ = c.Close()
}(conn)
}
}
func main() {
healthStatus = true
var listener net.Listener
var isListening = false
for {
logrus.Debug("Performing checks...")
healthStatus = healthcheck()
if healthStatus {
if !isListening {
isListening = true
//Restart listener
go listen(listener, config.port)
}
}
if !healthStatus {
if isListening {
isListening = false
}
}
time.Sleep(time.Second * 10)
}
}
EDIT :
With channel
package main
import (
"net"
"strconv"
"time"
)
var listening = make(chan bool)
func listen(l net.Listener, port int) {
var err error
l, err = net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
panic(err)
}
defer l.Close()
for {
localstatus := <- listening
if localstatus == false {
_ = l.Close()
return
}
conn, _ := l.Accept()
go func(c net.Conn) {
// Shut down the connection.
_ = c.Close()
listening <- true
}(conn)
}
}
func main() {
healthStatus := true
var listener net.Listener
var isListening = false
for {
healthStatus = healthcheck()
if healthStatus {
if !isListening {
isListening = true
//Restart listener
go listen(listener, config.port)
}
listening <- true
}
if !healthStatus {
if isListening {
isListening = false
listening <- false
}
}
time.Sleep(time.Second * 10)
}
}
Close the listener when the health goes bad. Use a channel to signal the accept loop that it's a clean shutdown.
var listener net.Listener
var done chan struct{}
for {
if healthcheck() {
if listener == nil {
var err error
listener, err = net.Listen("tcp", ":"+strconv.Itoa(conig.port))
if err != nil {
panic(err)
}
done = make(chan struct{})
go accept(listener, done)
}
} else {
if listener != nil {
close(done)
listener.Close()
done = nil
listener = nil
}
}
time.Sleep(time.Second * 10)
}
The accept function is:
func accept(l net.Listener, done chan struct{}) {
for {
conn, err := l.Accept()
select {
case <-done:
return
default:
}
if err != nil {
panic(err)
}
go func(c net.Conn) {
_ = c.Close()
}(conn)
}
}
So basically I'm writing a go test for my chat application and for some reason the if I write Test_saveMessage function in the top of this file my tests go through and they work fine, however if I write the Test_InitRouter in the top of this file - my server opens and the test doesn't finish. As if it would be listening for more requests. Does anyone know the reason of why this could be happening? Here is the that does not work code:
package messenger
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"strings"
"testing"
)
var testMessage = Message{
Username: "Name",
Message: "Test message"}
//Tests InitRouter both sending and receiving messages
func Test_InitRouter(t *testing.T) {
var receivedMessage Message
//Create test server with the InitRouter handler
s := httptest.NewServer(InitRouter())
defer s.Close()
// Convert URL from http to ws
u := "ws" + strings.TrimPrefix(s.URL, "http")
fmt.Println(u)
// Connect to the test server
ws, _, err := websocket.DefaultDialer.Dial(u, nil)
if err != nil {
t.Fatalf("%v", err)
}
defer ws.Close()
//Send message to the server read received message and see if it's the same
if err != ws.WriteJSON(testMessage) {
t.Fatalf("%v", err)
}
err = ws.ReadJSON(&receivedMessage)
if err != nil {
t.Fatalf("%v", err)
}
if receivedMessage != testMessage {
t.Fatalf("%v", err)
}
}
//Test for the saveMessage function
func Test_saveMessage(t *testing.T) {
saveMessage(testMessage)
assert.Equal(t, 1, len(messages), "Expected to have 1 message")
}
As soon as I move the Test_saveMessage function to the top it starts working properly.
Here is the code for the handler:
package messenger
import (
"fmt"
"github.com/go-chi/chi"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"net/http"
)
func InitRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", GetWebsocket)
return r
}
var clients = make(map[*websocket.Conn]bool) // connected clients
var broadcast = make(chan Message) // broadcast channel
var messages = []Message{}
func GetWebsocket(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a websocket
upgrader := websocket.Upgrader{}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error(err)
}
// Close the connection when the function returns
defer ws.Close()
// Register our new client and send him the chat history
clients[ws] = true
serveInitialMessages(ws)
//initialize message sending logic
sendMessages(ws)
}
// Sends messages from a particular websocket to the channel
func sendMessages(ws *websocket.Conn){
for {
var msg Message
// Read in a new message as JSON and map it to a Message object
err := ws.ReadJSON(&msg)
if err != nil {
log.Info(err)
delete(clients, ws)
break
}
// Send the newly received message to the broadcast channel
broadcast <- msg
saveMessage(msg)
}
}
func HandleMessages() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast
fmt.Println(msg)
// Send it out to every client that is currently connected
for client := range clients {
err := client.WriteJSON(msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients, client)
}
}
}
}
func saveMessage(m Message) {
if len(messages) >= 50 {
messages = messages[1:]
}
messages = append(messages, m)
}
func serveInitialMessages(ws *websocket.Conn) {
for _, m := range messages {
err := ws.WriteJSON(m)
if err != nil {
log.Error(err)
}
}
}
I use high-level structures to create a ws server.
func wsHandler(w http.ResponseWriter, r *http.Request) {
re := regexp.MustCompile(`^\/ws\/([0-9a-zA-Z]+)\/*`)
match := re.FindStringSubmatch(r.URL.Path)
if len(match) != 2 {
http.NotFound(w, r)
return
}
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
log.Printf("gobwas/ws: %s", err)
}
go func() {
defer conn.Close()
channels.Lock()
channels.Channels[match[1]] = append(channels.Channels[match[1]], &conn)
channels.Unlock()
for {
msg, op, err := wsutil.ReadClientData(conn)
if err != nil {
if err != io.EOF {
log.Printf("gobwas/ws/wsutil: %s", err)
}
break
}
if len(msg) > 0 {
go sendMessage(&conn, op, match[1], msg)
}
}
deleteConn(&conn, match[1])
}()
}
Then, when I implement a graceful disconnection, then with the usual conn.Close (), the connection simply breaks outside the protocol. I use the following code to close:
func onShutdown() {
channels.RLock()
for _, channel := range channels.Channels {
for _, conn := range channel {
if err := ws.WriteFrame(*conn, ws.NewCloseFrame(ws.NewCloseFrameBody(ws.StatusNormalClosure, "Server shutdown"))); err != nil {
fmt.Println(err)
}
(*conn).Close()
}
}
channels.RUnlock()
}
And even if I wait for a response from the client and then close the connection, then anyway, the client fixes the disconnection. (The client is the browser and JS WebSocket).
Sample JS code:
var socket = new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws/valid`);
socket.onclose = function (event) {
if (event.wasClean) {
console.log('Clean');
} else {
console.log('disconnect');
}
console.log('Code: ' + event.code + ' reason: ' + event.reason);
};