• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

GO语言--socket.io

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

socket.io是对websocket的封装以及扩展, 可以跨平台使用, 具体可看官网..

GO语言实现:

package main

import (
    "github.com/googollee/go-socket.io"
    "github.com/nicholaskh/log4go"
    "net/http"

    "os"
    "time"
    "path"
    "strings"
    "runtime"
    "fmt"
)
var gLogger log4go.Logger;


//do init before all others
func initAll(){
    gLogger = nil

    initLogger()
}

//de-init for all
func deinitAll(){
    if(nil == gLogger) {
        gLogger.Close();
        gLogger = nil
    }
}

func main()  {
    initAll()

    server, err := socketio.NewServer(nil)
    if err != nil {
        gLogger.Warn("启动服务器错误")
    }

    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"] = "你好"
            e := so.Emit("cn1111", m)
            //这个没有问题
            fmt.Println("\n\n")

            b := make(map[string]string)
            b["u-a"] = "中文内容" //这个不能是中文
            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 example
        // The return type may vary depending on whether you will return
        // For this example it is "string" type
        so.On("chat message with ack", func(msg string) string {
            return msg
        })
        so.On("disconnection", func() {
            gLogger.Info("on disconnect")
        })

    })

    server.On("error", func(so socketio.Socket, err error) {
        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(":5000", nil)
    deinitAll()
}

// GetCurFilename
// Get current file name, without suffix
func 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)

    return filenameOnly
}

//init for logger
func initLogger(){
    var filenameOnly string
    filenameOnly = GetCurFilename()
    var logFilename string =  filenameOnly + ".log";

    //gLogger = log4go.NewLogger()
    gLogger = make(log4go.Logger)
    //for console
    //gLogger.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter())
    gLogger.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter())
    //for log file
    if _, 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
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
GO语言练习:构建json 和 解析JSON 实例发布时间:2022-07-10
下一篇:
Go语言反射之类型反射发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap