美文网首页Golang 学习笔记
使用 Go + WebSockets 创建实时聊天服务

使用 Go + WebSockets 创建实时聊天服务

作者: 与蟒唯舞 | 来源:发表于2018-10-15 17:20 被阅读42次
安装第三方库
go get github.com/gorilla/websocket
服务端

src/main.go

package main

import (
    "github.com/gorilla/websocket"
    "log"
    "net/http"
)

// 使用字典结构更容易追加和删除内容
var clients = make(map[*websocket.Conn]bool) // 保存连接的客户端
var broadcast = make(chan Message)           // 消息广播通道

// 配置 upgrader
// 其中包含获取正常HTTP连接并将其升级到WebSocket的方法
var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return true
    },
}

// 定义 message 结构
type Message struct {
    Email    string `json:"email"`
    Username string `json:"username"`
    Message  string `json:"message"`
}

func handleConnections(w http.ResponseWriter, r *http.Request) {
    // 将初始GET请求升级到websocket
    ws, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal(err)
    }
    // 确保在函数返回时关闭连接
    defer ws.Close()
    // 将新的客户端连接添加到字典中
    clients[ws] = true

    for {
        var msg Message
        // 反序列化
        err := ws.ReadJSON(&msg)
        if err != nil {
            log.Printf("read error: %v", err)
            delete(clients, ws)
            break
        }
        // 将新接收的消息发送到广播通道
        broadcast <- msg
    }
}

func handleMessages() {
    for {
        // 从广播通道中获取消息
        msg := <-broadcast
        // 将其发送给当前连接的每个客户端
        for ws := range clients {
            err := ws.WriteJSON(msg)
            if err != nil {
                log.Printf("write error: %v", err)
                ws.Close()
                delete(clients, ws)
            }
        }
    }
}

func main() {
    // 创建简单的静态文件服务器
    http.Handle("/", http.FileServer(http.Dir("../public")))
    // 配置 websocket route
    http.HandleFunc("/ws", handleConnections)

    go handleMessages()

    log.Println("http server started on :8000")
    err := http.ListenAndServe(":8000", nil)
    if err != nil {
        log.Fatal("ListenAndServe error: ", err)
    }
}
客户端

public/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Chat</title>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/emojione/2.2.6/assets/css/emojione.min.css"/>
    <link rel="stylesheet" href="./style.css">

</head>
<body>
<header>
    <nav>
        <div class="nav-wrapper">
            <a href="/" class="brand-logo right">Simple Chat</a>
        </div>
    </nav>
</header>
<main id="app">
    <div class="row">
        <div class="col s12">
            <div class="card horizontal">
                <div id="chat-messages" class="card-content" v-html="chatContent">
                </div>
            </div>
        </div>
    </div>
    <div class="row" v-if="joined">
        <div class="input-field col s8">
            <input type="text" v-model="newMsg" @keyup.enter="send">
        </div>
        <div class="input-field col s4">
            <button class="waves-effect waves-light btn" @click="send">
                <i class="material-icons right">chat</i>
                Send
            </button>
        </div>
    </div>
    <div class="row" v-if="!joined">
        <div class="input-field col s8">
            <input type="email" v-model.trim="email" placeholder="Email">
        </div>
        <div class="input-field col s8">
            <input type="text" v-model.trim="username" placeholder="Username">
        </div>
        <div class="input-field col s4">
            <button class="waves-effect waves-light btn" @click="join()">
                <i class="material-icons right">done</i>
                Join
            </button>
        </div>
    </div>
</main>
<footer class="page-footer">
</footer>
<script src="https://unpkg.com/vue@2.1.3/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/emojione/2.2.6/lib/js/emojione.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<script src="./app.js"></script>
</body>
</html>

public/app.js

new Vue({
    el: '#app',
    data: {
        ws: null, // Our websocket
        newMsg: '', // Holds new messages to be sent to the server
        chatContent: '', // A running list of chat messages displayed on the screen
        email: null, // Email address used for grabbing an avatar
        username: null, // Our username
        joined: false // True if email and username have been filled in
    },
    created: function() {
        var self = this;
        this.ws = new WebSocket('ws://' + window.location.host + '/ws');
        this.ws.addEventListener('message', function(e) {
            var msg = JSON.parse(e.data);
            self.chatContent += '<div class="chip">'
                + '<img src="' + self.gravatarURL(msg.email) + '">' // Avatar
                + msg.username
                + '</div>'
                + emojione.toImage(msg.message) + '<br/>'; // Parse emojis

            var element = document.getElementById('chat-messages');
            element.scrollTop = element.scrollHeight; // Auto scroll to the bottom
        });
    },
    methods: {
        send: function () {
            if (this.newMsg != '') {
                this.ws.send(
                    JSON.stringify({
                            email: this.email,
                            username: this.username,
                            // 使用一些jQuery技巧来避免任何传入消息中的HTML和JavaScript
                            // 这可以防止任何类型的注入攻击
                            message: $('<p>').html(this.newMsg).text()
                        }
                    ));
                // 重置 newMsg
                this.newMsg = '';
            }
        },
        join: function () {
            if (!this.email) {
                Materialize.toast('You must enter an email', 2000);
                return
            }
            if (!this.username) {
                Materialize.toast('You must choose a username', 2000);
                return
            }
            this.email = $('<p>').html(this.email).text();
            this.username = $('<p>').html(this.username).text();
            this.joined = true;
        },
        gravatarURL: function(email) {
            return 'http://www.gravatar.com/avatar/' + CryptoJS.MD5(email);
        }
    }
});

public/style.css

body {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
}

main {
    flex: 1 0 auto;
}

#chat-messages {
    min-height: 10vh;
    height: 60vh;
    width: 100%;
    overflow-y: scroll;
}
目录结构
运行

进入到 src 目录下执行:

$ go run main.go

打开浏览器,输入:
http://localhost:8000

参考地址:https://scotch.io/bar-talk/build-a-realtime-chat-server-with-go-and-websockets

相关文章

网友评论

    本文标题:使用 Go + WebSockets 创建实时聊天服务

    本文链接:https://www.haomeiwen.com/subject/hnafzftx.html