Changed SessionID format.
Pusher libs are validating the sessionID format. So I need to conform with its format.
This commit is contained in:
+1
-1
@@ -226,7 +226,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionID := utils.RandomHash()
|
||||
sessionID := utils.GenerateSessionID()
|
||||
|
||||
if err := onOpen(conn, w, r, sessionID, app); err != nil {
|
||||
emitWSError(err, conn)
|
||||
|
||||
+7
-9
@@ -6,10 +6,11 @@ package utils
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
|
||||
@@ -22,12 +23,9 @@ func HashMAC(message, key []byte) string {
|
||||
}
|
||||
|
||||
// Generate a new random Hash
|
||||
func RandomHash() string {
|
||||
b := make([]byte, 25)
|
||||
func GenerateSessionID() string {
|
||||
MAX := 999999999
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic("websockets: Could not generate a random session ID")
|
||||
}
|
||||
|
||||
return base32.StdEncoding.EncodeToString(b)
|
||||
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateSession(t *testing.T) {
|
||||
sessionID := GenerateSessionID()
|
||||
|
||||
fmt.Println(sessionID)
|
||||
if matched, _ := regexp.MatchString("^\\d+\\.\\d+$", sessionID); !matched {
|
||||
t.Errorf("Must match ^\\d+\\.\\d+$, value: '%s'", sessionID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user