Go实战--golang中使用WebSocket实时聊天室(gorilla/websocket、nkovacs/go-socket.io)

前端之家收集整理的这篇文章主要介绍了Go实战--golang中使用WebSocket实时聊天室(gorilla/websocket、nkovacs/go-socket.io)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

生命不止,继续 go go go!!!

其实,早就应该跟大家分享golang中关于websocket的使用,但是一直不知道从何入手,也不能够很清晰的描述出来。

今天就浅尝辄止,通过第三方库实现websocket。

WebSocket

WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
WebSocket通信协议于2011年被IETF定为标准RFC 6455,并被RFC7936所补充规范。

WebSocket协议支持(在受控环境中运行不受信任的代码的)客户端与(选择加入该代码的通信的)远程主机之间进行全双工通信。用于此的安全模型是Web浏览器常用的基于原始的安全模式。 协议包括一个开放的握手以及随后的TCP层上的消息帧。 该技术的目标是为基于浏览器的、需要和服务器进行双向通信的(服务器不能依赖于打开多个HTTP连接(例如,使用XMLHttpRequest或iframe和长轮询))应用程序提供一种通信机制。

gorilla/websocket

A WebSocket implementation for Go.

github地址:
https://github.com/gorilla/websocket

Star: 4307

获取

go get github.com/gorilla/websocket

Server

其中,用到了satori/go.uuid:
Go实战–golang生成uuid(The way to go)

server.go

package main

import (
    "encoding/json"
    "fmt"
    "net/http"

    "github.com/gorilla/websocket"
    "github.com/satori/go.uuid"
)

type ClientManager struct {
    clients    map[*Client]bool
    broadcast  chan []byte
    register   chan *Client
    unregister chan *Client
}

type Client struct {
    id     string
    socket *websocket.Conn
    send   chan []byte
}

type Message struct {
    Sender    string `json:"sender,omitempty"`
    Recipient string `json:"recipient,omitempty"`
    Content   string `json:"content,omitempty"`
}

var manager = ClientManager{
    broadcast:  make(chan []byte),register:   make(chan *Client),unregister: make(chan *Client),clients:    make(map[*Client]bool),}

func (manager *ClientManager) start() {
    for {
        select {
        case conn := <-manager.register:
            manager.clients[conn] = true
            jsonMessage,_ := json.Marshal(&Message{Content: "/A new socket has connected."})
            manager.send(jsonMessage,conn)
        case conn := <-manager.unregister:
            if _,ok := manager.clients[conn]; ok {
                close(conn.send)
                delete(manager.clients,conn)
                jsonMessage,_ := json.Marshal(&Message{Content: "/A socket has disconnected."})
                manager.send(jsonMessage,conn)
            }
        case message := <-manager.broadcast:
            for conn := range manager.clients {
                select {
                case conn.send <- message:
                default:
                    close(conn.send)
                    delete(manager.clients,conn)
                }
            }
        }
    }
}

func (manager *ClientManager) send(message []byte,ignore *Client) {
    for conn := range manager.clients {
        if conn != ignore {
            conn.send <- message
        }
    }
}

func (c *Client) read() {
    defer func() {
        manager.unregister <- c
        c.socket.Close()
    }()

    for {
        _,message,err := c.socket.ReadMessage()
        if err != nil {
            manager.unregister <- c
            c.socket.Close()
            break
        }
        jsonMessage,_ := json.Marshal(&Message{Sender: c.id,Content: string(message)})
        manager.broadcast <- jsonMessage
    }
}

func (c *Client) write() {
    defer func() {
        c.socket.Close()
    }()

    for {
        select {
        case message,ok := <-c.send:
            if !ok {
                c.socket.WriteMessage(websocket.CloseMessage,[]byte{})
                return
            }

            c.socket.WriteMessage(websocket.TextMessage,message)
        }
    }
}

func main() {
    fmt.Println("Starting application...")
    go manager.start()
    http.HandleFunc("/ws",wsPage)
    http.ListenAndServe(":12345",nil)
}

func wsPage(res http.ResponseWriter,req *http.Request) {
    conn,error := (&websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}).Upgrade(res,req,nil)
    if error != nil {
        http.NotFound(res,req)
        return
    }
    client := &Client{id: uuid.NewV4().String(),socket: conn,send: make(chan []byte)}

    manager.register <- client

    go client.read()
    go client.write()
}

Go Client

package main

import (
    "flag"
    "fmt"
    "net/url"
    "time"

    "github.com/gorilla/websocket"
)

var addr = flag.String("addr","localhost:12345","http service address")

func main() {
    u := url.URL{Scheme: "ws",Host: *addr,Path: "/ws"}
    var dialer *websocket.Dialer

    conn,_,err := dialer.Dial(u.String(),nil)
    if err != nil {
        fmt.Println(err)
        return
    }

    go timeWriter(conn)

    for {
        _,err := conn.ReadMessage()
        if err != nil {
            fmt.Println("read:",err)
            return
        }

        fmt.Printf("received: %s\n",message)
    }
}

func timeWriter(conn *websocket.Conn) {
    for {
        time.Sleep(time.Second * 2)
        conn.WriteMessage(websocket.TextMessage,[]byte(time.Now().Format("2006-01-02 15:04:05")))
    }
}

HTML Client

test_websocket.html

<html>
<head>
<title>Golang Chat</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"> $(function() { var conn; var msg = $("#msg"); var log = $("#log"); function appendLog(msg) { var d = log[0] var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight; msg.appendTo(log) if (doScroll) { d.scrollTop = d.scrollHeight - d.clientHeight; } } $("#form").submit(function() { if (!conn) { return false; } if (!msg.val()) { return false; } conn.send(msg.val()); msg.val(""); return false }); if (window["WebSocket"]) { conn = new WebSocket("ws://localhost:12345/ws"); conn.onclose = function(evt) { appendLog($("<div><b>Connection Closed.</b></div>")) } conn.onmessage = function(evt) { appendLog($("<div/>").text(evt.data)) } } else { appendLog($("<div><b>WebSockets Not Support.</b></div>")) } }); </script>
<style type="text/css"> html { overflow: hidden; } body { overflow: hidden; padding: 0; margin: 0; width: 100%; height: 100%; background: gray; } #log { background: white; margin: 0; padding: 0.5em 0.5em 0.5em 0.5em; position: absolute; top: 0.5em; left: 0.5em; right: 0.5em; bottom: 3em; overflow: auto; } #form { padding: 0 0.5em 0 0.5em; margin: 0; position: absolute; bottom: 1em; left: 0px; width: 100%; overflow: hidden; } </style>
</head>
<body>
<div id="log"></div>
<form id="form">
    <input type="submit" value="发送" />
    <input type="text" id="msg" size="64"/>
</form>
</body>
</html>

运行:
运行server,运行client,浏览器打开html文件

实时聊天室

下面分享一个基于websocket的实时聊天室:
https://github.com/scotch-io/go-realtime-chat

main.go:

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/websocket"
)

var clients = make(map[*websocket.Conn]bool) // connected clients
var broadcast = make(chan Message)           // broadcast channel

// Configure the upgrader
var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return true
    },}

// Define our message object
type Message struct {
    Email    string `json:"email"`
    Username string `json:"username"`
    Message  string `json:"message"`
}

func main() {
    // Create a simple file server
    fs := http.FileServer(http.Dir("../public"))
    http.Handle("/",fs)

    // Configure websocket route
    http.HandleFunc("/ws",handleConnections)

    // Start listening for incoming chat messages
    go handleMessages()

    // Start the server on localhost port 8000 and log any errors
    log.Println("http server started on :8000")
    err := http.ListenAndServe(":8000",nil)
    if err != nil {
        log.Fatal("ListenAndServe: ",err)
    }
}

func handleConnections(w http.ResponseWriter,r *http.Request) {
    // Upgrade initial GET request to a websocket
    ws,err := upgrader.Upgrade(w,r,nil)
    if err != nil {
        log.Fatal(err)
    }
    // Make sure we close the connection when the function returns
    defer ws.Close()

    // Register our new client
    clients[ws] = true

    for {
        var msg Message
        // Read in a new message as JSON and map it to a Message object
        err := ws.ReadJSON(&msg)
        if err != nil {
            log.Printf("error: %v",err)
            delete(clients,ws)
            break
        }
        // Send the newly received message to the broadcast channel
        broadcast <- msg
    }
}

func handleMessages() {
    for {
        // Grab the next message from the broadcast channel
        msg := <-broadcast
        // Send it out to every client that is currently connected
        for client := range clients {
            err := client.WriteJSON(msg)
            if err != nil {
                log.Printf("error: %v",err)
                client.Close()
                delete(clients,client)
            }
        }
    }
}

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,message: $('<p>').html(this.newMsg).text() // Strip out html } )); this.newMsg = ''; // Reset 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);
        }
    }
});

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>

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; }

nkovacs/go-socket.io

github地址:
https://github.com/nkovacs/go-socket.io

获取
go get github.com/nkovacs/go-socket.io

main.go

package main

import (
    "log"
    "net/http"

    "github.com/nkovacs/go-socket.io"
)

func main() {
    server,err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.On("connection",func(so socketio.Socket) {
        log.Println("on connection")
        so.Join("chat")
        so.On("chat message",func(msg string) {
            log.Println("emit:",so.Emit("chat message",msg))
            so.BroadcastTo("chat","chat message",msg)
        })
        so.On("disconnection",func() {
            log.Println("on disconnect")
        })
    })
    server.On("error",func(so socketio.Socket,err error) {
        log.Println("error:",err)
    })

    http.Handle("/socket.io/",server)
    http.Handle("/",http.FileServer(http.Dir("./public")))
    log.Println("Serving at localhost:12345...")
    log.Fatal(http.ListenAndServe(":12345",nil))
}

index.html

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style> * { margin: 0; padding: 0; Box-sizing: border-Box; } body { font: 13px Helvetica,Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130,224,255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script> var socket = io(); $('form').submit(function(){ socket.emit('chat message',$('#m').val()); $('#m').val(''); return false; }); socket.on('chat message',function(msg){ $('#messages').append($('<li>').text(msg)); }); </script>
  </body>
</html>

浏览器输入:
http://localhost:12345/

猜你在找的Go相关文章