add 1 on 1 chat
This commit is contained in:
+122
-94
@@ -2,11 +2,14 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -15,68 +18,75 @@ import (
|
|||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// chatServer enables broadcasting to a set of subscribers.
|
// chatMessage is the payload sent from the frontend to /publish.
|
||||||
|
type chatMessage struct {
|
||||||
|
To string `json:"to"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
From string `json:"from,omitempty"` // optional, server fills this in
|
||||||
|
}
|
||||||
|
|
||||||
|
// chatServer enables 1:1 messaging between named subscribers.
|
||||||
type chatServer struct {
|
type chatServer struct {
|
||||||
// subscriberMessageBuffer controls the max number
|
// subscriberMessageBuffer controls the max number
|
||||||
// of messages that can be queued for a subscriber
|
// of messages that can be queued for a subscriber
|
||||||
// before it is kicked.
|
// before it is kicked.
|
||||||
//
|
|
||||||
// Defaults to 16.
|
|
||||||
subscriberMessageBuffer int
|
subscriberMessageBuffer int
|
||||||
|
|
||||||
// publishLimiter controls the rate limit applied to the publish endpoint.
|
// publishLimiter controls the rate limit applied to the publish endpoint.
|
||||||
//
|
|
||||||
// Defaults to one publish every 100ms with a burst of 8.
|
|
||||||
publishLimiter *rate.Limiter
|
publishLimiter *rate.Limiter
|
||||||
|
|
||||||
// logf controls where logs are sent.
|
// logf controls where logs are sent.
|
||||||
// Defaults to log.Printf.
|
|
||||||
logf func(f string, v ...any)
|
logf func(f string, v ...any)
|
||||||
|
|
||||||
// serveMux routes the various endpoints to the appropriate handler.
|
// serveMux routes the various endpoints.
|
||||||
serveMux http.ServeMux
|
serveMux http.ServeMux
|
||||||
|
|
||||||
subscribersMu sync.Mutex
|
// subscribersMu protects the subscribers map.
|
||||||
subscribers map[*subscriber]struct{}
|
// Key is the username (from query param).
|
||||||
|
subscribersMu sync.RWMutex
|
||||||
|
subscribers map[string]*subscriber
|
||||||
}
|
}
|
||||||
|
|
||||||
// newChatServer constructs a chatServer with the defaults.
|
// newChatServer constructs a chatServer with the defaults.
|
||||||
func newChatServer() *chatServer {
|
func newChatServer() *chatServer {
|
||||||
cs := &chatServer{
|
cs := &chatServer{
|
||||||
subscriberMessageBuffer: 16,
|
subscriberMessageBuffer: 16,
|
||||||
logf: log.Printf,
|
logf: slog.Info,
|
||||||
subscribers: make(map[*subscriber]struct{}),
|
subscribers: make(map[string]*subscriber),
|
||||||
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
|
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
|
||||||
}
|
}
|
||||||
// Serve static files (index.html, index.js, index.css) from cwd
|
|
||||||
cs.serveMux.Handle("/", http.FileServer(http.Dir(".")))
|
cs.serveMux.Handle("/", http.FileServer(http.Dir(".")))
|
||||||
// WebSocket endpoint: clients connect here to receive messages
|
|
||||||
cs.serveMux.HandleFunc("/subscribe", cs.subscribeHandler)
|
cs.serveMux.HandleFunc("/subscribe", cs.subscribeHandler)
|
||||||
// HTTP POST endpoint: clients POST here to send a message
|
|
||||||
cs.serveMux.HandleFunc("/publish", cs.publishHandler)
|
cs.serveMux.HandleFunc("/publish", cs.publishHandler)
|
||||||
|
cs.serveMux.HandleFunc("/users", cs.usersHandler) // list online users
|
||||||
|
|
||||||
return cs
|
return cs
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscriber represents a single connected client.
|
// subscriber represents a single connected client.
|
||||||
// Messages are sent on the msgs channel. If the client
|
|
||||||
// cannot keep up (buffer full), closeSlow is called
|
|
||||||
// to disconnect them.
|
|
||||||
type subscriber struct {
|
type subscriber struct {
|
||||||
msgs chan []byte
|
username string // unique identifier from query param
|
||||||
closeSlow func()
|
msgs chan []byte // buffered channel for outbound messages
|
||||||
|
closeSlow func() // kicks the subscriber if buffer fills
|
||||||
|
cancel context.CancelFunc // cancels the read context on disconnect
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP makes chatServer implement http.Handler,
|
// ServeHTTP delegates to the internal serveMux.
|
||||||
// delegating to the internal serveMux.
|
|
||||||
func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
cs.serveMux.ServeHTTP(w, r)
|
cs.serveMux.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribeHandler accepts the WebSocket connection and then subscribes
|
// subscribeHandler extracts the username from query params and upgrades to WebSocket.
|
||||||
// it to all future messages.
|
|
||||||
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
|
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
err := cs.subscribe(w, r)
|
// Extract username from ?user= parameter
|
||||||
|
username := r.URL.Query().Get("user")
|
||||||
|
if username == "" {
|
||||||
|
http.Error(w, "missing user parameter", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
username = url.PathEscape(strings.TrimSpace(username))
|
||||||
|
|
||||||
|
err := cs.subscribe(w, r, username)
|
||||||
if errors.Is(err, context.Canceled) {
|
if errors.Is(err, context.Canceled) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -85,48 +95,20 @@ func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cs.logf("%v", err)
|
cs.logf("subscribe error for user %s: %v", username, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// publishHandler reads the request body with a limit of 8192 bytes
|
// subscribe upgrades the HTTP connection to WebSocket and registers the subscriber.
|
||||||
// and then publishes the received message.
|
func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request, username string) error {
|
||||||
func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != "POST" {
|
|
||||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
body := http.MaxBytesReader(w, r.Body, 8192)
|
|
||||||
msg, err := io.ReadAll(body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cs.publish(msg)
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusAccepted)
|
|
||||||
}
|
|
||||||
|
|
||||||
// subscribe upgrades the HTTP connection to a WebSocket,
|
|
||||||
// registers the subscriber, and then loops forever writing
|
|
||||||
// messages from the subscriber's channel to the WebSocket.
|
|
||||||
//
|
|
||||||
// It uses CloseRead to keep reading control frames (close, ping, pong)
|
|
||||||
// and cancel the context if the connection drops. This means we
|
|
||||||
// never actually read message data from the WebSocket —
|
|
||||||
// messages come in via /publish instead.
|
|
||||||
func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request) error {
|
|
||||||
// We need a mutex here because the WebSocket connection might
|
|
||||||
// be closed by closeSlow (from another goroutine) before we've
|
|
||||||
// assigned it to c. The mutex ensures we don't race.
|
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
var c *websocket.Conn
|
var c *websocket.Conn
|
||||||
var closed bool
|
var closed bool
|
||||||
|
|
||||||
s := &subscriber{
|
s := &subscriber{
|
||||||
msgs: make(chan []byte, cs.subscriberMessageBuffer),
|
username: username,
|
||||||
|
msgs: make(chan []byte, cs.subscriberMessageBuffer),
|
||||||
closeSlow: func() {
|
closeSlow: func() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
@@ -137,19 +119,17 @@ func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request) error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the subscriber BEFORE accepting the WebSocket.
|
// Register BEFORE accepting WebSocket
|
||||||
// This means we won't miss any messages published between
|
|
||||||
// the accept and the registration.
|
|
||||||
cs.addSubscriber(s)
|
cs.addSubscriber(s)
|
||||||
defer cs.deleteSubscriber(s)
|
defer cs.deleteSubscriber(s)
|
||||||
|
|
||||||
// Upgrade HTTP to WebSocket
|
// Upgrade to WebSocket
|
||||||
c2, err := websocket.Accept(w, r, nil)
|
c2, err := websocket.Accept(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if closeSlow was already called before we got the connection
|
// Check if already closed
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
if closed {
|
if closed {
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
@@ -160,68 +140,116 @@ func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request) error {
|
|||||||
|
|
||||||
defer c.CloseNow()
|
defer c.CloseNow()
|
||||||
|
|
||||||
// CloseRead returns a context that is cancelled when:
|
// Create context that cancels on close/read error
|
||||||
// - The client sends a close frame
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
// - The connection drops
|
s.cancel = cancel
|
||||||
// This frees us from manually handling ping/pong and deadlines.
|
|
||||||
ctx := c.CloseRead(context.Background())
|
// CloseRead handles ping/pong/close frames automatically
|
||||||
|
connCtx := c.CloseRead(ctx)
|
||||||
|
|
||||||
|
cs.logf("user %s connected", username)
|
||||||
|
|
||||||
// Main loop: write messages to the WebSocket as they arrive
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-s.msgs:
|
case msg := <-s.msgs:
|
||||||
err := writeTimeout(ctx, time.Second*5, c, msg)
|
err := writeTimeout(connCtx, time.Second*5, c, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-connCtx.Done():
|
||||||
// Context cancelled = client disconnected
|
cs.logf("user %s disconnected", username)
|
||||||
return ctx.Err()
|
cancel() // trigger cleanup
|
||||||
|
return connCtx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// publish sends a message to ALL subscribers.
|
// publishHandler reads the JSON message and routes it to the recipient.
|
||||||
// It never blocks — if a subscriber's buffer is full,
|
func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// they get kicked via closeSlow.
|
if r.Method != http.MethodPost {
|
||||||
func (cs *chatServer) publish(msg []byte) {
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||||
cs.subscribersMu.Lock()
|
return
|
||||||
defer cs.subscribersMu.Unlock()
|
}
|
||||||
|
|
||||||
|
body := http.MaxBytesReader(w, r.Body, 8192)
|
||||||
|
data, err := io.ReadAll(body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var msg chatMessage
|
||||||
|
if err := json.Unmarshal(data, &msg); err != nil {
|
||||||
|
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.To == "" || msg.Message == "" {
|
||||||
|
http.Error(w, "missing 'to' or 'message' field", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deliver the message to the recipient
|
||||||
|
delivered := cs.deliverMessage(msg.To, msg.Message)
|
||||||
|
|
||||||
|
if delivered {
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "recipient not found", http.StatusNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deliverMessage sends a message to a specific recipient.
|
||||||
|
func (cs *chatServer) deliverMessage(toUsername, message string) bool {
|
||||||
|
cs.subscribersMu.RLock()
|
||||||
|
recipient, ok := cs.subscribers[toUsername]
|
||||||
|
cs.subscribersMu.RUnlock()
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Rate limit: wait for a token before broadcasting
|
|
||||||
cs.publishLimiter.Wait(context.Background())
|
cs.publishLimiter.Wait(context.Background())
|
||||||
|
|
||||||
for s := range cs.subscribers {
|
select {
|
||||||
select {
|
case recipient.msgs <- []byte(message):
|
||||||
case s.msgs <- msg:
|
return true
|
||||||
// Message delivered to subscriber's buffer
|
default:
|
||||||
default:
|
// Buffer full — kick slow client
|
||||||
// Buffer is full — kick the slow subscriber
|
go recipient.closeSlow()
|
||||||
go s.closeSlow()
|
return false
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// usersHandler returns list of currently connected usernames.
|
||||||
|
func (cs *chatServer) usersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cs.subscribersMu.RLock()
|
||||||
|
users := make([]string, 0, len(cs.subscribers))
|
||||||
|
for username := range cs.subscribers {
|
||||||
|
users = append(users, username)
|
||||||
|
}
|
||||||
|
cs.subscribersMu.RUnlock()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(users)
|
||||||
|
}
|
||||||
|
|
||||||
// addSubscriber registers a subscriber.
|
// addSubscriber registers a subscriber.
|
||||||
func (cs *chatServer) addSubscriber(s *subscriber) {
|
func (cs *chatServer) addSubscriber(s *subscriber) {
|
||||||
cs.subscribersMu.Lock()
|
cs.subscribersMu.Lock()
|
||||||
cs.subscribers[s] = struct{}{}
|
cs.subscribers[s.username] = s
|
||||||
cs.subscribersMu.Unlock()
|
cs.subscribersMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteSubscriber removes a subscriber.
|
// deleteSubscriber removes a subscriber.
|
||||||
func (cs *chatServer) deleteSubscriber(s *subscriber) {
|
func (cs *chatServer) deleteSubscriber(s *subscriber) {
|
||||||
cs.subscribersMu.Lock()
|
cs.subscribersMu.Lock()
|
||||||
delete(cs.subscribers, s)
|
delete(cs.subscribers, s.username)
|
||||||
cs.subscribersMu.Unlock()
|
cs.subscribersMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeTimeout writes a message to the WebSocket with a deadline.
|
|
||||||
// If the write doesn't complete within the timeout, the context
|
|
||||||
// is cancelled and the operation aborts.
|
|
||||||
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
|
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
return c.Write(ctx, websocket.MessageText, msg)
|
return c.Write(ctx, websocket.MessageText, msg)
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-4
@@ -13,13 +13,52 @@ body {
|
|||||||
color: #e0e0e0;
|
color: #e0e0e0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#messages {
|
.header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
background: #0f0f23;
|
||||||
|
border-bottom: 1px solid #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-setup {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
#username {
|
||||||
|
padding: 0.4rem;
|
||||||
|
background: #16213e;
|
||||||
|
border: 1px solid #333;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: #e0e0e0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#login-btn {
|
||||||
|
padding: 0.4rem 0.8rem;
|
||||||
|
background: #6d4aff;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.online-users {
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
#online-list {
|
||||||
|
color: #4ade80;
|
||||||
|
}
|
||||||
|
|
||||||
|
#message-log {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.message {
|
#message-log p {
|
||||||
padding: 0.4rem 0.6rem;
|
padding: 0.4rem 0.6rem;
|
||||||
margin-bottom: 0.3rem;
|
margin-bottom: 0.3rem;
|
||||||
background: #16213e;
|
background: #16213e;
|
||||||
@@ -32,9 +71,10 @@ body {
|
|||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
background: #0f0f23;
|
background: #0f0f23;
|
||||||
border-top: 1px solid #333;
|
border-top: 1px solid #333;
|
||||||
|
gap: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
#message-input {
|
#recipient-select {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
background: #16213e;
|
background: #16213e;
|
||||||
@@ -44,8 +84,17 @@ body {
|
|||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#message-input {
|
||||||
|
flex: 2;
|
||||||
|
padding: 0.5rem;
|
||||||
|
background: #16213e;
|
||||||
|
border: 1px solid #333;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: #e0e0e0;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
#publish-form button {
|
#publish-form button {
|
||||||
margin-left: 0.5rem;
|
|
||||||
padding: 0.5rem 1rem;
|
padding: 0.5rem 1rem;
|
||||||
background: #6d4aff;
|
background: #6d4aff;
|
||||||
color: white;
|
color: white;
|
||||||
|
|||||||
+13
-2
@@ -7,10 +7,21 @@
|
|||||||
<link rel="stylesheet" href="index.css">
|
<link rel="stylesheet" href="index.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="header">
|
||||||
|
<div class="user-setup">
|
||||||
|
<input type="text" id="username" placeholder="Your username" value="">
|
||||||
|
<button id="login-btn">Join Chat</button>
|
||||||
|
</div>
|
||||||
|
<div class="online-users">
|
||||||
|
<strong>Online:</strong> <span id="online-list"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="message-log"></div>
|
<div id="message-log"></div>
|
||||||
|
|
||||||
<form id="publish-form">
|
<form id="publish-form" style="display:none;">
|
||||||
<input type="text" id="message-input" placeholder="Type a message..." autocomplete="off" autofocus>
|
<input type="text" id="recipient-select" placeholder="Recipient..." autocomplete="off">
|
||||||
|
<input type="text" id="message-input" placeholder="Type a message..." autocomplete="off" autofocus disabled>
|
||||||
<button type="submit">Send</button>
|
<button type="submit">Send</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|||||||
+97
-28
@@ -1,22 +1,54 @@
|
|||||||
(() => {
|
(() => {
|
||||||
|
let ws = null
|
||||||
let expectingMessage = false
|
let expectingMessage = false
|
||||||
|
const currentUserEl = document.getElementById('username')
|
||||||
|
const loginBtn = document.getElementById('login-btn')
|
||||||
|
const messageLog = document.getElementById('message-log')
|
||||||
|
const publishForm = document.getElementById('publish-form')
|
||||||
|
const recipientSelect = document.getElementById('recipient-select')
|
||||||
|
const messageInput = document.getElementById('message-input')
|
||||||
|
const onlineList = document.getElementById('online-list')
|
||||||
|
|
||||||
function dial() {
|
// Auto-fill username from URL parameter
|
||||||
const conn = new WebSocket(`ws://${location.host}/ws/subscribe`)
|
const urlParams = new URLSearchParams(window.location.search)
|
||||||
|
const urlUser = urlParams.get('user')
|
||||||
|
if (urlUser) {
|
||||||
|
currentUserEl.value = urlUser
|
||||||
|
}
|
||||||
|
|
||||||
conn.addEventListener('close', ev => {
|
// Login button handler
|
||||||
appendLog(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`, true)
|
loginBtn.addEventListener('click', () => {
|
||||||
if (ev.code !== 1001) {
|
const username = currentUserEl.value.trim()
|
||||||
appendLog('Reconnecting in 1s', true)
|
if (!username) {
|
||||||
setTimeout(dial, 1000)
|
appendLog('Please enter a username', true)
|
||||||
}
|
return
|
||||||
})
|
}
|
||||||
|
|
||||||
conn.addEventListener('open', ev => {
|
// Connect to WebSocket with username
|
||||||
|
connect(username)
|
||||||
|
|
||||||
|
// Show publish form, hide login
|
||||||
|
loginBtn.style.display = 'none'
|
||||||
|
currentUserEl.style.display = 'none'
|
||||||
|
publishForm.style.display = 'flex'
|
||||||
|
messageInput.disabled = false
|
||||||
|
messageInput.focus()
|
||||||
|
|
||||||
|
appendLog(`Joined as ${username}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
function connect(username) {
|
||||||
|
const encodedUser = encodeURIComponent(username)
|
||||||
|
ws = new WebSocket(`ws://${location.host}/ws/subscribe?user=${encodedUser}`)
|
||||||
|
|
||||||
|
ws.addEventListener('open', () => {
|
||||||
console.info('WebSocket connected')
|
console.info('WebSocket connected')
|
||||||
|
refreshUsers()
|
||||||
|
// Refresh users every 5 seconds
|
||||||
|
setInterval(refreshUsers, 5000)
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.addEventListener('message', ev => {
|
ws.addEventListener('message', (ev) => {
|
||||||
if (typeof ev.data !== 'string') {
|
if (typeof ev.data !== 'string') {
|
||||||
console.error('unexpected message type', typeof ev.data)
|
console.error('unexpected message type', typeof ev.data)
|
||||||
return
|
return
|
||||||
@@ -27,47 +59,84 @@
|
|||||||
expectingMessage = false
|
expectingMessage = false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ws.addEventListener('close', (ev) => {
|
||||||
|
appendLog(`Disconnected (code: ${ev.code})`, true)
|
||||||
|
if (ev.code !== 1001) {
|
||||||
|
appendLog('Reconnecting...', true)
|
||||||
|
setTimeout(() => connect(username), 2000)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ws.addEventListener('error', (err) => {
|
||||||
|
console.error('WebSocket error:', err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
dial()
|
function refreshUsers() {
|
||||||
|
fetch('/ws/users')
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(users => {
|
||||||
|
onlineList.textContent = users.join(', ') || 'none'
|
||||||
|
|
||||||
const messageLog = document.getElementById('message-log')
|
// Update recipient dropdown
|
||||||
const publishForm = document.getElementById('publish-form')
|
recipientSelect.innerHTML = ''
|
||||||
const messageInput = document.getElementById('message-input')
|
if (users.length > 0) {
|
||||||
|
users.forEach(user => {
|
||||||
|
const opt = document.createElement('option')
|
||||||
|
opt.value = user
|
||||||
|
opt.textContent = user
|
||||||
|
recipientSelect.appendChild(opt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('Failed to refresh users:', err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function appendLog(text, error) {
|
function appendLog(text, error = false) {
|
||||||
const p = document.createElement('p')
|
const p = document.createElement('p')
|
||||||
p.innerText = `${new Date().toLocaleTimeString()}: ${text}`
|
const time = new Date().toLocaleTimeString()
|
||||||
|
p.innerText = `[${time}] ${text}`
|
||||||
if (error) {
|
if (error) {
|
||||||
p.style.color = 'red'
|
p.style.color = '#ff6b6b'
|
||||||
p.style.fontStyle = 'bold'
|
|
||||||
}
|
}
|
||||||
messageLog.append(p)
|
messageLog.append(p)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
appendLog('Submit a message to get started!')
|
// Submit message
|
||||||
|
publishForm.onsubmit = async (ev) => {
|
||||||
publishForm.onsubmit = async ev => {
|
|
||||||
ev.preventDefault()
|
ev.preventDefault()
|
||||||
|
|
||||||
const msg = messageInput.value
|
const recipient = recipientSelect.value.trim()
|
||||||
if (msg === '') {
|
const msg = messageInput.value.trim()
|
||||||
|
|
||||||
|
if (!recipient || !msg) {
|
||||||
|
appendLog('Please select a recipient and enter a message', true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
messageInput.value = ''
|
|
||||||
|
|
||||||
|
messageInput.value = ''
|
||||||
expectingMessage = true
|
expectingMessage = true
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const payload = JSON.stringify({ to: recipient, message: msg })
|
||||||
const resp = await fetch('/ws/publish', {
|
const resp = await fetch('/ws/publish', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: msg,
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: payload,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (resp.status !== 202) {
|
if (resp.status !== 202) {
|
||||||
throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`)
|
const text = await resp.text()
|
||||||
|
throw new Error(`Failed: ${text}`)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
appendLog(`Publish failed: ${err.message}`, true)
|
appendLog(`Send failed: ${err.message}`, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
appendLog('Enter your username to join the chat')
|
||||||
})()
|
})()
|
||||||
|
|||||||
Reference in New Issue
Block a user