-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
47 lines (37 loc) · 911 Bytes
/
Copy pathclient.go
File metadata and controls
47 lines (37 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Dial() from x/net/websocket is used to connect to the ws server.
NOTE: server is in /cmd/ws/main.go
*/
package main
import (
"log"
"golang.org/x/net/websocket"
)
type Message struct{
RoomId string
Data string
}
func main() {
origin := "http://localhost/"
url := "ws://localhost:8080/ws" // server url
ws, err := websocket.Dial(url, "", origin) // try connecting
// on failure to open connection
if err != nil {
log.Fatal(err)
}
// upon successful connection send a string
message := Message{
RoomId: "abc",
Data: "Hey there, this message is from client",
}
if err := websocket.JSON.Send(ws, message); err != nil {
log.Fatal("Error in sending: ",err)
}
// count of bytes and message is recieved
var recievedMsg Message
if err = websocket.JSON.Receive(ws,&recievedMsg); err != nil {
log.Fatal(err)
}
log.Println(recievedMsg.RoomId)
log.Println(recievedMsg.Data)
}