diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 92bb53a..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 zekro Tutorial Repositories - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index bf88d86..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# goWebSocketTutorial -Repo zu dem Go WebSocket Tutorial diff --git a/assets/index.html b/assets/index.html index e41515b..61b29af 100644 --- a/assets/index.html +++ b/assets/index.html @@ -3,18 +3,17 @@ - Websocket Test + WebSocket Tutorial - - + - + - \ No newline at end of file diff --git a/events.go b/events.go index c67e39c..af6fe50 100644 --- a/events.go +++ b/events.go @@ -4,27 +4,28 @@ import ( "encoding/json" ) -// EventHandler defines a function which gets passed -// the event as instance pointer type EventHandler func(*Event) -// Event contains events name and data +/* +EXAMPLE EVENT: +{ + "event": "message", + "data": "this is a data object" +} +*/ + type Event struct { Name string `json:"event"` Data interface{} `json:"data"` } -// NewEventFromRaw creates an event object -// from raw binary JSON data -func NewEventFromRaw(rawData []byte) (*Event, error) { - eve := &Event{} - err := json.Unmarshal(rawData, eve) - return eve, err +func NewEventFronRaw(rawData []byte) (*Event, error) { + event := new(Event) + err := json.Unmarshal(rawData, event) + return event, err } -// Raw creates raw binary JSON data from event -// instance func (e *Event) Raw() []byte { raw, _ := json.Marshal(e) return raw -} \ No newline at end of file +} diff --git a/main.go b/main.go index c5a64d8..f840ae0 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "log" "net/http" + "strings" ) func main() { @@ -12,16 +13,19 @@ func main() { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { ws, err := NewWebSocket(w, r) if err != nil { - panic(err) + log.Println("Error creating websocket connection: %v", err) + return } ws.On("message", func(e *Event) { - log.Printf("[MESSAGE] %v", e.Data) + log.Printf("Message received: %s", e.Data.(string)) ws.Out <- (&Event{ Name: "response", - Data: e.Data, + Data: strings.ToUpper(e.Data.(string)), }).Raw() }) }) + log.Println("WebServer listening on port 8080") http.ListenAndServe(":8080", nil) + } diff --git a/websocket.go b/websocket.go index bc0295f..4bbdd23 100644 --- a/websocket.go +++ b/websocket.go @@ -7,10 +7,6 @@ import ( "github.com/gorilla/websocket" ) -const ( - MAX_MSG_SIZE = 5000 -) - var upgrader = websocket.Upgrader{ ReadBufferSize: 2048, WriteBufferSize: 2048, @@ -29,10 +25,10 @@ type WebSocket struct { func NewWebSocket(w http.ResponseWriter, r *http.Request) (*WebSocket, error) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - log.Printf("[ERROR | SOCKET CONNECT] %v", err) + log.Printf("An error occured while upgrading the conntion: %v", err) return nil, err } - // conn.SetWriteDeadline(time.Now().Add(MSG_TIMEOUT)) + ws := &WebSocket{ Conn: conn, Out: make(chan []byte), @@ -48,20 +44,19 @@ func (ws *WebSocket) Reader() { defer func() { ws.Conn.Close() }() - ws.Conn.SetReadLimit(MAX_MSG_SIZE) for { _, message, err := ws.Conn.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { - log.Printf("[ERROR] %v", err) + log.Printf("WS Message Error: %v", err) } break } - event, err := NewEventFromRaw(message) + event, err := NewEventFronRaw(message) if err != nil { - log.Printf("[ERROR | MSG] %v", err) + log.Printf("Error parsing message: %v", err) } else { - log.Printf("[MSG] %v", event) + log.Printf("MSG: %v", event) } if action, ok := ws.Events[event.Name]; ok { action(event) @@ -74,7 +69,7 @@ func (ws *WebSocket) Writer() { select { case message, ok := <-ws.Out: if !ok { - ws.Conn.WriteMessage(websocket.CloseMessage, []byte{}) + ws.Conn.WriteMessage(websocket.CloseMessage, make([]byte, 0)) return } w, err := ws.Conn.NextWriter(websocket.TextMessage) @@ -87,7 +82,7 @@ func (ws *WebSocket) Writer() { } } -func (ws *WebSocket) On(event string, action EventHandler) *WebSocket { - ws.Events[event] = action +func (ws *WebSocket) On(eventName string, action EventHandler) *WebSocket { + ws.Events[eventName] = action return ws }