Compare commits

..

No commits in common. "main" and "v0.0.6" have entirely different histories.
main ... v0.0.6

4 changed files with 18 additions and 27 deletions

View File

@ -7,7 +7,7 @@ A minimal Go WebSocket relay server with SSL support for P2P connections.
```bash
go mod tidy
# Configure via config.yaml (see config.yaml for options)
go run main.go --config-file=./config.yaml
go run main.go
```
## Configuration
@ -18,16 +18,16 @@ Edit `config.yaml` to configure:
## Usage
- WebSocket endpoint: `/`
- WebSocket endpoint: `/ws`
- All WebSocket messages are relayed to all connected clients
## Testing
```javascript
// For TLS enabled (default config)
const ws = new WebSocket('wss://localhost:8443/');
const ws = new WebSocket('wss://localhost:8443/ws');
// For HTTP only
// const ws = new WebSocket('ws://localhost:8443/');
// const ws = new WebSocket('ws://localhost:8443/ws');
ws.onmessage = (event) => console.log('Received:', event.data);
ws.send('Hello from client!');
```

View File

@ -3,8 +3,4 @@ server:
tls:
enabled: true
cert_file: cert.pem
key_file: key.pem
metrics:
enabled: true
port: 9090
key_file: key.pem

View File

@ -11,18 +11,18 @@ var (
Help: "Number of currently connected WebSocket clients",
})
MessagesTotal = promauto.NewGauge(prometheus.GaugeOpts{
Name: "websocket_message",
Help: "Number of WebSocket messages processed",
MessagesTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "websocket_messages_total",
Help: "Total number of WebSocket messages processed",
})
ConnectionsTotal = promauto.NewGauge(prometheus.GaugeOpts{
Name: "websocket_connection",
Help: "Number of WebSocket connections established",
ConnectionsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "websocket_connections_total",
Help: "Total number of WebSocket connections established",
})
DisconnectionsTotal = promauto.NewGauge(prometheus.GaugeOpts{
Name: "websocket_disconnection",
Help: "Number of WebSocket disconnections",
DisconnectionsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "websocket_disconnections_total",
Help: "Total number of WebSocket disconnections",
})
)
)

11
main.go
View File

@ -1,22 +1,17 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"websocket-relay/internal/config"
"websocket-relay/internal/hub"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
configFile := flag.String("config-file", "config.yaml", "Path to configuration file")
flag.Parse()
cfg, err := config.Load(*configFile)
cfg, err := config.Load("config.yaml")
if err != nil {
log.Fatal("Failed to load config:", err)
}
@ -35,7 +30,7 @@ func main() {
}
mux := http.NewServeMux()
mux.HandleFunc("/", h.HandleWebSocket)
mux.HandleFunc("/ws", h.HandleWebSocket)
addr := fmt.Sprintf(":%d", cfg.Server.Port)
if cfg.Server.TLS.Enabled {