Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>PP - Websockets</title>
</head>
/**
* Once the website loads, we want to apply listeners and connect to websocket
* */
window.onload = function () {
// Apply our listener functions to the submit event on both forms
// we do it this way to avoid redirects
document.getElementById("chatroom-selection").onsubmit = changeChatRoom;
document.getElementById("chatroom-message").onsubmit = sendMessage;
// Check if the browser supports WebSocket
// readMessages will start the client to read messages and handle them
// appropriatly.
// This is suppose to be ran as a goroutine
func (c *Client) readMessages() {
defer func() {
// Graceful Close the Connection once this
// function is done
c.manager.removeClient(c)
}()
// Loop Forever
@percybolmer
percybolmer / websockets-client-v4.go
Created September 7, 2022 13:27
write websockets messages
// writeMessages is a process that listens for new messages to output to the Client
func (c *Client) writeMessages() {
defer func() {
// Graceful close if this triggers a closing
c.manager.removeClient(c)
}()
for {
select {
case message, ok := <-c.egress:
// Client is a websocket client, basically a frontend visitor
type Client struct {
// the websocket connection
connection *websocket.Conn
// manager is the manager used to manage the client
manager *Manager
// egress is used to avoid concurrent writes on the WebSocket
egress chan []byte
}
// serveWS is a HTTP Handler that the has the Manager that allows connections
func (m *Manager) serveWS(w http.ResponseWriter, r *http.Request) {
log.Println("New connection")
// Begin by upgrading the HTTP request
conn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
/**
* sendMessage will send a new message onto the Websocket
* */
function sendMessage() {
var newmessage = document.getElementById("message");
if (newmessage != null) {
conn.send(newmessage.value);
}
return false;
}
// readMessages will start the client to read messages and handle them
// appropriatly.
// This is suppose to be ran as a goroutine
func (c *Client) readMessages() {
defer func() {
// Graceful Close the Connection once this
// function is done
c.manager.removeClient(c)
}()
// Loop Forever
@percybolmer
percybolmer / websockets-manager-v2.go
Last active September 6, 2022 17:27
Websockets with remove/add clients
package main
import (
"log"
"net/http"
"sync"
"github.com/gorilla/websocket"
)
@percybolmer
percybolmer / websockets-client-v1.go
Created September 6, 2022 06:16
Websocket clients
package main
import "github.com/gorilla/websocket"
// ClientList is a map used to help manage a map of clients
type ClientList map[*Client]bool
// Client is a websocket client, basically a frontend visitor
type Client struct {
// the websocket connection