golang 聊天室

lucifer_qiao · · 5337 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

最近看了一些关于golang的资料,发现它的WEBSOCKT实现真起来真的很容易,下面是代码

go.net/websocket是需要安装的,这个自己装上就行

package main

import (
    "container/list"
    "fmt"
    "go.net/websocket"
    "io"
    "net/http"
)

var connid int
var conns *list.List

func Chat(ws *websocket.Conn) {
    defer ws.Close()
    item := conns.PushBack(ws)
    defer conns.Remove(item)

    var err error
    for {
        var data string
        if err = websocket.Message.Receive(ws, &data); err != nil {
            fmt.Printf("disconnected\n")
            break
        }

        SendMessage(item, fmt.Sprintf("%s", data))
    }
}

func SendMessage(self *list.Element, data string) {
    //for _, item := range conns {
    for item := conns.Front(); item != nil; item = item.Next() {
        ws, ok := item.Value.(*websocket.Conn)
        if !ok {
            panic("item not *websocket.Conn")
        }

        if item == self {
            continue
        }

        io.WriteString(ws, data)
    }
}

// 客户端默认显示页面
func Client(w http.ResponseWriter, r *http.Request) {
    html := `<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>golang websocket chatroom</title>
    <script language="javascript"type="text/javascript">  
        var sock=null; 
        var wsuri ="ws://192.168.1.163:7878/chat"; //这里的IP如果是局域测试的话,需要换成自己的
        window.onload = function(){
            console.log("onload");
            sock = new WebSocket(wsuri);
            sock.onopen=function(e){
                console.log("connected to "+wsuri);
            }
            sock.onclose=function(e){
                console.log("connection closed (" + e.code + ")");
            }
            sock.onmessage=function(e){
                console.log("message received: " + e.data);
                document.getElementById("list").innerHTML += e.data;
                var div = document.getElementById("list")
                div.scrollTop = div.scrollHeight; 
            }
        }
        function send () {
            var who = document.getElementById('who').value;
            if (who.length == 0){
                document.getElementById('who').focus();
                return
            }
            var msg = document.getElementById('msg').value;
            if (msg.length == 0){
                document.getElementById('msg').focus();
                return
            }
            var data = who + "  say:  " + msg + "<br/><br/>";
            document.getElementById('msg').value="";
            document.getElementById('who').value="";
            var div = document.getElementById("list")
            div.innerHTML += data;
            div.scrollTop = div.scrollHeight; 
            sock.send(data);
        }        
    </script>
</head>
<body>
    <div id="list" style="height: 300px;overflow-y: scroll;border: 1px solid #CCC;">
    </div>
    <div>
        who are you 
        <input type="text" id="who" size="60" />
        your message
        <input type="text" id="msg" size="60" />

        <button onclick="send()">send</button>
    </div>
</body>
</html>`
    io.WriteString(w, html)
}

func main() {
    conns = list.New()

    http.Handle("/chat", websocket.Handler(Chat))
    http.HandleFunc("/", Client)
    err := http.ListenAndServe(":7878", nil)
    if err != nil {
        panic("ListenAndServe: " + err.Error())
    }
}



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:lucifer_qiao

查看原文:golang 聊天室

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

5337 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传