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
Socket.io for golang #3242
Comments
Anyone ? |
I also need golang client for socket.io nodejs server. |
crickets |
Need Golang client too, and also need Rust client. |
Yeah, I definitely would love to see Socket.IO supporting golang as well. :) @mofadeyunduo If you're asking whether to use Socket.IO or Websockets alone, I've written an answer on StackOverflow out of my own experience with both of them. If you have the option to make a microservice in NodeJS, or use a Socket.IO library in another language, it's definitely worth it. |
Need Golang client too |
Do need Golang client, too. And I'd like to contribute to it, as soon as I can. |
totally agree with bro |
I tried this out and achieved a base result on my local package main
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func handleConnection(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
fmt.Println("Client connected")
for {
messageType, p, err := conn.ReadMessage()
if err != nil {
fmt.Println(err)
return
}
if err := conn.WriteMessage(messageType, p); err != nil {
fmt.Println(err)
return
}
}
}
func main() {
http.HandleFunc("/ws", handleConnection)
serverAddr := "localhost:8080"
fmt.Printf("WebSocket server is running on ws://%s\n", serverAddr)
err := http.ListenAndServe(serverAddr, nil)
if err != nil {
fmt.Println(err)
}
} This was my client: package main
import (
"fmt"
"log"
"net/url"
"github.com/gorilla/websocket"
)
func main() {
// Define the WebSocket server URL
serverURL := "ws://localhost:8080/ws"
// Parse the URL
u, err := url.Parse(serverURL)
if err != nil {
log.Fatal(err)
}
// Establish a WebSocket connection
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
fmt.Println("Connected to WebSocket server")
// Start a goroutine to read messages from the server
go readMessages(conn)
// Send a message to the server
message := "Hello, Server!"
err = conn.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("Error sending message:", err)
return
}
// Keep the program running
select {}
}
func readMessages(conn *websocket.Conn) {
for {
messageType, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Error reading message:", err)
return
}
if messageType == websocket.TextMessage {
handleMessage(msg)
}
}
}
func handleMessage(msg []byte) {
fmt.Println("Received message:", string(msg))
} Output
Of course this is just a basic example. I will need to perhaps create a public repo so that others can contribute to the same / develop it further. If anyone is interested - my current public github repo |
Note: for support questions, please use one of these channels: stackoverflow or slack
You want to:
Current behaviour
googollee/go-socket.io#188
Steps to reproduce (if the current behaviour is a bug)
Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.
Expected behaviour
Setup
Other information (e.g. stacktraces, related issues, suggestions how to fix)
The text was updated successfully, but these errors were encountered: