Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f69355d69d | ||
|
|
9ad38190a5 | ||
|
|
983c4195e0 |
@ -7,7 +7,7 @@ A minimal Go WebSocket relay server with SSL support for P2P connections.
|
|||||||
```bash
|
```bash
|
||||||
go mod tidy
|
go mod tidy
|
||||||
# Configure via config.yaml (see config.yaml for options)
|
# Configure via config.yaml (see config.yaml for options)
|
||||||
go run main.go
|
go run main.go --config-file=./config.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
@ -18,16 +18,16 @@ Edit `config.yaml` to configure:
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
- WebSocket endpoint: `/ws`
|
- WebSocket endpoint: `/`
|
||||||
- All WebSocket messages are relayed to all connected clients
|
- All WebSocket messages are relayed to all connected clients
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// For TLS enabled (default config)
|
// For TLS enabled (default config)
|
||||||
const ws = new WebSocket('wss://localhost:8443/ws');
|
const ws = new WebSocket('wss://localhost:8443/');
|
||||||
// For HTTP only
|
// For HTTP only
|
||||||
// const ws = new WebSocket('ws://localhost:8443/ws');
|
// const ws = new WebSocket('ws://localhost:8443/');
|
||||||
ws.onmessage = (event) => console.log('Received:', event.data);
|
ws.onmessage = (event) => console.log('Received:', event.data);
|
||||||
ws.send('Hello from client!');
|
ws.send('Hello from client!');
|
||||||
```
|
```
|
||||||
@ -3,4 +3,8 @@ server:
|
|||||||
tls:
|
tls:
|
||||||
enabled: true
|
enabled: true
|
||||||
cert_file: cert.pem
|
cert_file: cert.pem
|
||||||
key_file: key.pem
|
key_file: key.pem
|
||||||
|
|
||||||
|
metrics:
|
||||||
|
enabled: true
|
||||||
|
port: 9090
|
||||||
|
|||||||
@ -11,18 +11,18 @@ var (
|
|||||||
Help: "Number of currently connected WebSocket clients",
|
Help: "Number of currently connected WebSocket clients",
|
||||||
})
|
})
|
||||||
|
|
||||||
MessagesTotal = promauto.NewCounter(prometheus.CounterOpts{
|
MessagesTotal = promauto.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: "websocket_messages_total",
|
Name: "websocket_message",
|
||||||
Help: "Total number of WebSocket messages processed",
|
Help: "Number of WebSocket messages processed",
|
||||||
})
|
})
|
||||||
|
|
||||||
ConnectionsTotal = promauto.NewCounter(prometheus.CounterOpts{
|
ConnectionsTotal = promauto.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: "websocket_connections_total",
|
Name: "websocket_connection",
|
||||||
Help: "Total number of WebSocket connections established",
|
Help: "Number of WebSocket connections established",
|
||||||
})
|
})
|
||||||
|
|
||||||
DisconnectionsTotal = promauto.NewCounter(prometheus.CounterOpts{
|
DisconnectionsTotal = promauto.NewGauge(prometheus.GaugeOpts{
|
||||||
Name: "websocket_disconnections_total",
|
Name: "websocket_disconnection",
|
||||||
Help: "Total number of WebSocket disconnections",
|
Help: "Number of WebSocket disconnections",
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|||||||
11
main.go
11
main.go
@ -1,17 +1,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"websocket-relay/internal/config"
|
"websocket-relay/internal/config"
|
||||||
"websocket-relay/internal/hub"
|
"websocket-relay/internal/hub"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg, err := config.Load("config.yaml")
|
configFile := flag.String("config-file", "config.yaml", "Path to configuration file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
cfg, err := config.Load(*configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to load config:", err)
|
log.Fatal("Failed to load config:", err)
|
||||||
}
|
}
|
||||||
@ -30,7 +35,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/ws", h.HandleWebSocket)
|
mux.HandleFunc("/", h.HandleWebSocket)
|
||||||
|
|
||||||
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
||||||
if cfg.Server.TLS.Enabled {
|
if cfg.Server.TLS.Enabled {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user