Last active
March 29, 2023 04:15
-
-
Save PankhudiB/e06c56bcd65c329e6996b611d118f7ed to your computer and use it in GitHub Desktop.
Websocket server writes to the connection. Websocket client reads message type and defers reading rest of the message.
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
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"github.com/gobwas/ws" | |
"github.com/gobwas/ws/wsutil" | |
"log" | |
) | |
func main() { | |
url := "ws://localhost:8080/talk-to-server" | |
fmt.Println("Starting WebSocket Client...") | |
fmt.Println("Dialing to WebSocket Server...\n\n") | |
ctx := context.Background() | |
conn, _, _, err := ws.Dial(ctx, url) | |
if err != nil { | |
log.Fatal(err.Error()) | |
} | |
dataBytes, _, err := wsutil.ReadServerData(conn) | |
if err != nil { | |
fmt.Println("Error reading from websocket connection ! ", err.Error()) | |
} | |
messageWrapper := MessageWrapper{} | |
err = json.Unmarshal(dataBytes, &messageWrapper) | |
if err != nil { | |
fmt.Println("Error unmarshalling...", err.Error()) | |
} | |
fmt.Println("Level 1 Unmarshalling...", "\n", "Type : ", messageWrapper.MessageType, "\n", "Content : ", messageWrapper.Content) | |
subMessage := SubMessage{} | |
err = json.Unmarshal(messageWrapper.Content, &subMessage) | |
if err != nil { | |
fmt.Println("Error ", err.Error()) | |
} | |
fmt.Println("Level 2 Unmarshalling...", "\n", "subMessage.Name : ", subMessage.Name, "\n", "subMessage.Place : ", subMessage.Place) | |
} | |
type MessageWrapper struct { | |
MessageType string `json:"message_type"` | |
Content json.RawMessage `json:"content"` | |
} | |
type SubMessage struct { | |
Name string `json:"name"` | |
Place string `json:"place"` | |
} |
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/gin-gonic/gin" | |
"github.com/gorilla/websocket" | |
"net/http" | |
) | |
func main() { | |
httpEngine := gin.Default() | |
fmt.Println("Starting WebSocket Server...") | |
httpEngine.GET("/talk-to-server", handleWebsocket) | |
fmt.Println() | |
err := http.ListenAndServe(":8080", httpEngine) | |
if err != nil { | |
fmt.Println("Error starting server!") | |
return | |
} | |
} | |
func handleWebsocket(context *gin.Context) { | |
upgrader := websocket.Upgrader{} | |
websocketConn, err := upgrader.Upgrade(context.Writer, context.Request, nil) | |
if err != nil { | |
fmt.Println("Error upgrading connection !", err.Error()) | |
} | |
messageA := MessageWrapper{ | |
MessageType: "A", | |
MessageType_A: MessageType_A{Name: "Pankhudi", Place: "India"}, | |
} | |
bytes, err := json.Marshal(messageA) | |
fmt.Println("Sending bytes :", string(bytes)) | |
err = websocketConn.WriteMessage(websocket.TextMessage, bytes) | |
if err != nil { | |
fmt.Println("Error writing to ws connection !", err.Error()) | |
} | |
} | |
type MessageType_A struct { | |
Name string `json:"name"` | |
Place string `json:"place"` | |
} | |
type MessageType_B struct { | |
Animal string `json:"animal"` | |
Thing string `json:"thing"` | |
} | |
type MessageWrapper struct { | |
MessageType string `json:"message_type"` | |
MessageType_A `json:"content"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment