From 31b804d90b9ece1962203f90c99338d188a293ff Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 25 Feb 2017 20:25:22 +0200 Subject: [PATCH] Replace websocket->Values() with ->SetValue/GetValue instead. Former-commit-id: fe7fa1a40123b34ec864732ddfee796574cefa72 --- adaptors/websocket/connection.go | 53 ++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/adaptors/websocket/connection.go b/adaptors/websocket/connection.go index 110b2965..44ee4ab6 100644 --- a/adaptors/websocket/connection.go +++ b/adaptors/websocket/connection.go @@ -143,8 +143,6 @@ type ( // websocket has everything you need to authenticate the user BUT if it's necessary // then you use it to receive user information, for example: from headers Context() *iris.Context - // Values returns the temporary lock-free connection's data store - Values() ConnectionValues // OnDisconnect registers a callback which fires when this connection is closed by an error or manual OnDisconnect(DisconnectFunc) @@ -176,6 +174,16 @@ type ( // Disconnect disconnects the client, close the underline websocket conn and removes it from the conn list // returns the error, if any, from the underline connection Disconnect() error + // SetValue sets a key-value pair on the connection's mem store. + SetValue(key string, value interface{}) + // GetValue gets a value by its key from the connection's mem store. + GetValue(key string) interface{} + // GetValueArrString gets a value as []string by its key from the connection's mem store. + GetValueArrString(key string) []string + // GetValueString gets a value as string by its key from the connection's mem store. + GetValueString(key string) string + // GetValueInt gets a value as integer by its key from the connection's mem store. + GetValueInt(key string) int } connection struct { @@ -468,3 +476,44 @@ func (c *connection) fireOnLeave(roomName string) { func (c *connection) Disconnect() error { return c.server.Disconnect(c.ID()) } + +// mem per-conn store + +func (c *connection) SetValue(key string, value interface{}) { + c.values.Set(key, value) +} + +func (c *connection) GetValue(key string) interface{} { + return c.values.Get(key) +} + +func (c *connection) GetValueArrString(key string) []string { + if v := c.values.Get(key); v != nil { + if arrString, ok := v.([]string); ok { + return arrString + } + } + return nil +} + +func (c *connection) GetValueString(key string) string { + if v := c.values.Get(key); v != nil { + if s, ok := v.(string); ok { + return s + } + } + return "" +} + +func (c *connection) GetValueInt(key string) int { + if v := c.values.Get(key); v != nil { + if i, ok := v.(int); ok { + return i + } else if s, ok := v.(string); ok { + if iv, err := strconv.Atoi(s); err == nil { + return iv + } + } + } + return 0 +}