use UUID instead of random

This commit is contained in:
giongto35 2019-04-22 00:43:42 +08:00
parent 281e044a7a
commit e22e5d3d69
2 changed files with 7 additions and 6 deletions

View file

@ -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
}

5
ws.go
View file

@ -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