Go Language--socket.io

Source: Internet
Author: User

Socket.io is the WebSocket package and extension, can be used across platforms, specifically crossing network.

Go language implementation:

Package Mainimport ("Github.com/googollee/go-socket.io"    "Github.com/nicholaskh/log4go"    "net/http"    "OS"    " Time"    "Path"    "Strings"    "Runtime"    "FMT") var Glogger log4go. Logger;// Doinit before all Othersfunc Initall () {Glogger=NilInitlogger ()}De-init forAllfunc Deinitall () {if(Nil==Glogger)        {Glogger.close (); Glogger=Nil}}func Main () {Initall () server, err:= Socketio. NewServer (Nil)    ifErr! =Nil{Glogger.warn ("Start Server Error")} server. On ("Connection", func (so socketio. Socket) {Glogger.info ("On connection") so. Join ("Chat") so. On ("Chat Message", func (msg string) {m:=Make (map[string]interface{}) m["a"] ="Hello"e:= So. Emit ("cn1111", M)//This is no problem with FMT. Println ("\ n") B:=Make (map[string]string) b["u-a"] ="Chinese content"//This can't be Chinese m["b-c"] =b e= So. Emit ("cn2222", M) glogger.info (e) glogger.info ("Emit:", so. Emit ("Chat Message", msg)) So. Broadcastto ("Chat","Chat Message", msg)}) //Socket.io Acknowledgement ExampleThereturn typeMay vary depending on whetherreturnFor the example it is"string" typeSo . On ("Chat message with ACK", func (msg String) string {returnmsg}) So. On ("disconnection", func () {Glogger.info ("On Disconnect")}) server. On ("Error", func (So socketio. Socket, errError) {Glogger.warn ("ERROR:", Err)}) http. Handle ("/socket.io/", server) http. Handle ("/", HTTP. Fileserver (http. Dir ("/users/deer_mac/downloads/go-socket.io-master/example/chat/public")) ) Glogger.info ("serving at localhost:5000 ...") http. Listenandserve (":",Nil) Deinitall ()}//Getcurfilename//Get Current file name, without Suffixfunc getcurfilename () string {_, Fullefilename, _, _:= Runtime. Caller (0)    //FMT. Println (Fullefilename) var filenamewithsuffix string Filenamewithsuffix=path. Base (Fullefilename)Fmt. Println ("filenamewithsuffix=", Filenamewithsuffix) var filesuffix string Filesuffix=path. EXT (Filenamewithsuffix)Fmt. Println ("filesuffix=", Filesuffix) var filenameonly string filenameonly=strings. Trimsuffix (Filenamewithsuffix, Filesuffix)Fmt. Println ("filenameonly=", Filenameonly)returnFilenameonly}Init forLoggerfunc Initlogger () {var filenameonly string filenameonly=getcurfilename () var logfilename string= Filenameonly +". Log"; Glogger =Log4go. Newlogger () Glogger=Make (Log4go. Logger)// forConsoleGlogger.addfilter ("stdout", Log4go.info, Log4go. Newconsolelogwriter ()) Glogger.addfilter ("stdout", Log4go.info, Log4go. Newconsolelogwriter ())// forlog Fileif_, Err: = OS. Stat (LogFileName); Err = =Nil {        Fmt. Printf ("found old log file%s, now remove it\n", LogFileName) os. Remove (LogFileName)}Glogger.addfilter ("logfile", Log4go. FINEST, Log4go. Newfilelogwriter (LogFileName,true)) Glogger.addfilter ("logfile", Log4go. FINEST, Log4go. Newfilelogwriter (LogFileName,false)) Glogger.info ("Current time is :%s", time. Now (). Format ("15:04:05 MST 2006/01/02"))    return}

Go Language--socket.io

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.