This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
439 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# csl-backend | ||
## Combat Surf League API | ||
# Combat Surf League API | ||
|
||
`go run .` <br> | ||
`go build -o bin/hello` <br> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package websocket | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
var upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 4096, | ||
WriteBufferSize: 4096, | ||
CheckOrigin: func(r *http.Request) bool { return true }, | ||
} | ||
|
||
type Client struct { | ||
conn *websocket.Conn | ||
wsServer *WsServer | ||
send chan []byte | ||
SteamID string `json:"steamid"` | ||
Name string `json:"name"` | ||
rooms map[*Room]bool | ||
} | ||
|
||
func NewClient(conn *websocket.Conn, wsServer *WsServer, name string, steamID string) *Client { | ||
client := &Client{ | ||
SteamID: steamID, | ||
Name: name, | ||
conn: conn, | ||
wsServer: wsServer, | ||
send: make(chan []byte, 256), | ||
rooms: make(map[*Room]bool), | ||
} | ||
return client | ||
} | ||
|
||
func ServeWs(w http.ResponseWriter, r *http.Request) { | ||
// get params | ||
name := r.URL.Query().Get("name") | ||
steamid := r.URL.Query().Get("steamid") | ||
|
||
if len(name) == 0 || len(steamid) == 0 { | ||
log.Println("no name or no steamid") | ||
return | ||
} | ||
|
||
// receive websocket connection | ||
conn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Println("smt went wrong") | ||
return | ||
} | ||
|
||
client := NewClient(conn, WS, name, steamid) | ||
|
||
// handle I/O for client | ||
go client.readPump() | ||
go client.writePump() | ||
|
||
WS.register <- client | ||
} | ||
|
||
func (client *Client) readPump() { | ||
defer client.disconnect() | ||
|
||
//client.conn.SetReadLimit(maxMessageSize) | ||
//client.conn.SetReadDeadline(time.Now().Add(pongWait)) | ||
//client.conn.SetPongHandler(func(string) error { client.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) | ||
|
||
// Start endless read loop, waiting for messages from client | ||
for { | ||
_, jsonMessage, err := client.conn.ReadMessage() | ||
if err != nil { | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | ||
log.Printf("unexpected close error: %v", err) | ||
} | ||
break | ||
} | ||
|
||
client.handleNewMessage(jsonMessage) | ||
} | ||
} | ||
|
||
func (client *Client) writePump() { | ||
defer client.conn.Close() | ||
for { | ||
select { | ||
case message, ok := <-client.send: | ||
log.Println("write pump") | ||
if !ok { | ||
// The WsServer closed the channel. | ||
client.conn.WriteMessage(websocket.CloseMessage, []byte{}) | ||
return | ||
} | ||
|
||
w, err := client.conn.NextWriter(websocket.TextMessage) | ||
if err != nil { | ||
return | ||
} | ||
w.Write(message) | ||
|
||
// Attach queued chat messages to the current websocket message. | ||
n := len(client.send) | ||
for i := 0; i < n; i++ { | ||
w.Write([]byte("\n")) | ||
w.Write(<-client.send) | ||
} | ||
|
||
if err := w.Close(); err != nil { | ||
return | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
func (client *Client) disconnect() { | ||
client.wsServer.unregister <- client | ||
for room := range client.rooms { | ||
room.unregister <- client | ||
} | ||
close(client.send) | ||
client.conn.Close() | ||
} | ||
|
||
func (client *Client) handleNewMessage(jsonMessage []byte) { | ||
var message Message | ||
if err := json.Unmarshal(jsonMessage, &message); err != nil { | ||
log.Printf("Error on unmarshal JSON message %s", err) | ||
return | ||
} | ||
|
||
log.Println("handle new message: ") | ||
log.Println(message) | ||
|
||
// send msg | ||
// join room | ||
// leave room | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Chat Example</title> | ||
<script type="text/javascript"> | ||
window.onload = function () { | ||
var conn; | ||
var msg = document.getElementById("msg"); | ||
var log = document.getElementById("log"); | ||
|
||
function appendLog(item) { | ||
var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1; | ||
log.appendChild(item); | ||
if (doScroll) { | ||
log.scrollTop = log.scrollHeight - log.clientHeight; | ||
} | ||
} | ||
|
||
document.getElementById("form").onsubmit = function () { | ||
if (!conn) { | ||
return false; | ||
} | ||
if (!msg.value) { | ||
return false; | ||
} | ||
|
||
var ya = { | ||
action: "a", | ||
message: msg.value, | ||
target: "idk", | ||
sender: { | ||
steamid: "roby", | ||
name: "pah ya", | ||
}, | ||
} | ||
console.log(JSON.stringify(ya)) | ||
conn.send(JSON.stringify(ya) ); | ||
msg.value = ""; | ||
return false; | ||
}; | ||
|
||
if (window["WebSocket"]) { | ||
let names = ["roby", "someone", "yaw", "dd", "brooks", "evai", "sirputin"] | ||
const n = names[Math.floor(Math.random() * names.length)]; | ||
conn = new WebSocket(`ws://localhost:8081/ws?name=${n}&steamid=123`); | ||
conn.onclose = function (evt) { | ||
var item = document.createElement("div"); | ||
item.innerHTML = "<b>Connection closed.</b>"; | ||
appendLog(item); | ||
}; | ||
conn.onmessage = function (evt) { | ||
var messages = evt.data.split('\n'); | ||
for (var i = 0; i < messages.length; i++) { | ||
var item = document.createElement("div"); | ||
item.innerText = messages[i]; | ||
appendLog(item); | ||
} | ||
}; | ||
} else { | ||
var item = document.createElement("div"); | ||
item.innerHTML = "<b>Your browser does not support WebSockets.</b>"; | ||
appendLog(item); | ||
} | ||
}; | ||
</script> | ||
<style type="text/css"> | ||
html { | ||
overflow: hidden; | ||
} | ||
|
||
body { | ||
overflow: hidden; | ||
padding: 0; | ||
margin: 0; | ||
width: 100%; | ||
height: 100%; | ||
background: gray; | ||
} | ||
|
||
#log { | ||
background: white; | ||
margin: 0; | ||
padding: 0.5em 0.5em 0.5em 0.5em; | ||
position: absolute; | ||
top: 0.5em; | ||
left: 0.5em; | ||
right: 0.5em; | ||
bottom: 3em; | ||
overflow: auto; | ||
} | ||
|
||
#form { | ||
padding: 0 0.5em 0 0.5em; | ||
margin: 0; | ||
position: absolute; | ||
bottom: 1em; | ||
left: 0px; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
|
||
</style> | ||
</head> | ||
<body> | ||
<div id="log"></div> | ||
<form id="form"> | ||
<input type="submit" value="Send" /> | ||
<input type="text" id="msg" size="64" autofocus /> | ||
</form> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package websocket | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
const SendMessageAction = "send-message" | ||
const JoinRoomAction = "join-room" | ||
const LeaveRoomAction = "leave-room" | ||
|
||
type Message struct { | ||
Action string `json:"action"` | ||
Message string `json:"message"` | ||
Target string `json:"target"` | ||
Sender *Client `json:"sender"` | ||
} | ||
|
||
func (message *Message) encode() []byte { | ||
json, err := json.Marshal(message) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
return json | ||
} |
Oops, something went wrong.