From e22e5d3d693993d5b8d510af765bf00aa5e6c0fe Mon Sep 17 00:00:00 2001 From: giongto35 Date: Mon, 22 Apr 2019 00:43:42 +0800 Subject: [PATCH] use UUID instead of random --- main.go | 8 +++++--- ws.go | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index a08f4068..c8e27093 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "math/rand" "net/http" _ "net/http/pprof" - "strconv" "sync" "time" @@ -20,6 +19,7 @@ import ( "github.com/giongto35/cloud-game/webrtc" "github.com/gorilla/websocket" pionRTC "github.com/pion/webrtc" + uuid "github.com/satori/go.uuid" ) const ( @@ -203,7 +203,8 @@ func ws(w http.ResponseWriter, r *http.Request) { // Create connection to overlord client := NewClient(c) - sessionID := strconv.Itoa(rand.Int()) + //sessionID := strconv.Itoa(rand.Int()) + sessionID := uuid.Must(uuid.NewV4()).String() wssession := &Session{ client: client, @@ -315,7 +316,8 @@ func ws(w http.ResponseWriter, r *http.Request) { // generateRoomID generate a unique room ID containing 16 digits func generateRoomID() string { - roomID := strconv.FormatInt(rand.Int63(), 16) + //roomID := strconv.FormatInt(rand.Int63(), 16) + roomID := uuid.Must(uuid.NewV4()).String() return roomID } diff --git a/ws.go b/ws.go index a8e34787..e1396d17 100644 --- a/ws.go +++ b/ws.go @@ -3,11 +3,10 @@ package main import ( "encoding/json" "log" - "math/rand" - "strconv" "time" "github.com/gorilla/websocket" + uuid "github.com/satori/go.uuid" ) type Client struct { @@ -47,7 +46,7 @@ func NewClient(conn *websocket.Conn) *Client { // send sends a packet and trigger callback when the packet comes back func (c *Client) send(request WSPacket, callback func(response WSPacket)) { - request.PacketID = strconv.Itoa(rand.Int()) + request.PacketID = uuid.Must(uuid.NewV4()).String() data, err := json.Marshal(request) if err != nil { return