This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"sync" | |
"github.com/gorilla/websocket" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |