Go with WebSocket's token of survival

Source: Internet
Author: User

1  Packagetoken2 3 Import (4"Crypto/md5"5"FMT"6"IO"7"Math/rand"8"Sync"9"Time"Ten ) One  A type User struct { - ID String -Timer *Time . Timer the } -  -var Token map[string]*user = Make (map[string]*User) -var maxlivetime time. Duration = time. Hour * 24//token update time is tentatively 24 hours + var lock sync. Rwmutex -  + ConstRandstringlen = 16 A  at func randstring () string { -str: = make ([]byte, Randstringlen) -      forI: = 0; i < Randstringlen; i++ { -Str[i] =byte(Rand. Uint32 () & ((1 << 7)-1)) -     } -     returnstring (str) in } -  toFunc New (UserID string, token *string, Writetoken func ()) { + lock. Lock () -Delete (Token, *token) theHash: =MD5. New () * io. WriteString (hash, UserID) $ io. WriteString (hash, time. Now (). String ())Panax Notoginseng*token = FMT. Sprintf ("%x", Hash. Sum (nil)) -Timer: =Time . Afterfunc (Maxlivetime, func () {New (UserID, token, Writetoken)}) theToken[*token] = &user{ + Id:userid, A Timer:timer, the     } + lock. Unlock () - Writetoken () $ } $  - func Del (token string) { - lock. Lock () the defer lock. Unlock () -User: =Token[token]Wuyi user. Timer.stop () the Delete (token, token) - } Wu  -Func GetUser (token string) (user *User, existoken bool) { About lock. Rlock () $ defer lock. Runlock () -User, Existoken =Token[token] -     returnUser, Existoken -}
token
1  PackageNetwork2 3 Import (4"Logs"5"Time"6"Token"7 8"Github.com/gorilla/websocket"9 )Ten  One type clientdata struct { AConn *WebSocket. Conn - Token string - } the  -var wsclients =Make (map[string]clientdata) -  - //is online + func isOnline (ClientID string) bool { - lock. Rlock () +_, IsOnline: =Wsclients[clientid] A lock. Runlock () at     returnIsOnline - } -  - //---------------------------------------------Connection Management Section -  - //Increase WebSocket Connection inFunc addclient (ClientID string, con *WebSocket. Conn) (canadd bool) { -  to     ifIsOnline (ClientID) { + writeresponsetoclient (ClientID, serverresponse{ -Method: "/wslogin", the Code:noticeforcequit, * Message:mforcequit, $ Result:nil,Panax Notoginseng         }) - deleteclient (ClientID) theTime. Sleep (Time.millisecond)//If you do not sleep or sleep too short, two connections will be turned off at the same time. + logs. Print (ClientID, Mforcequit) A addconnection (ClientID, con) the         return false +     } - addconnection (ClientID, con) $     return true $ } -  -Func addconnection (ClientID string, con *WebSocket. Conn) { theClientdata: =clientdata{ - Conn:con,WuyiToken: "", the     } -Token. New (ClientID, &clientdata.token, func () {//This function is used to write tokens back to the client after each token change Wu         //after updating the client to Writeresponse, pay attention to deadlock - lock. Lock () AboutWsclients[clientid] =Clientdata $ lock. Unlock () - writeresponsetoclient (ClientID, serverresponse{ - code:success, -Method: "/token", AMessage: "", +result:map[string]string{"Token": Clientdata.token}, the         }) -     }) $ } the  the //Get client Connections theFunc getclient (ClientID string) (Clientconn *WebSocket. Conn, isOnline bool) { the  - lock. Rlock () inClientdata, OK: =Wsclients[clientid] the lock. Runlock () the     returnClientdata.conn, OK About } the  the //Remove WebSocket Connection the func deleteclient (ClientID string) { +Clientdata, OK: =Wsclients[clientid] -     if!OK { the         returnBayi     } the lock. Lock () the clientData.conn.Close () - token. Del (Clientdata.token) - Delete (wsclients, ClientID) the lock. Unlock () the}
WebSocket Connection Management

Because login is using WebSocket, return to the front end token is also Websoket (Websoket verify success immediately return token), so websocket in, token is in, WebSocket close connection token also ended the life cycle

Go with WebSocket's token of survival

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.