Golang——WebSocket

栏目: 后端 · 发布时间: 6年前

内容简介:拉模式的缺点 数据更新频率低,则大多数的请求是无效的在线用户数量多,则服务端的查询负载高。定时查询拉取,无法满足时效性要求

拉模式与推送模式

拉模式的缺点 数据更新频率低,则大多数的请求是无效的

在线用户数量多,则服务端的查询负载高。

定时查询拉取,无法满足时效性要求

推送模式 尽在数据更新才推送,需要维护大量的在线长连接,数据更新后立即推送。

WebSocket推送

浏览器支持的socket编程,轻松维护服务端长连接,基于TCP可靠传输之上的协议,无需开发者关心通讯细节。提供了高度抽象的编程接口,业务开发成本低。

websocket协议

Golang——WebSocket

来源于慕课网

协议升级后,继续复用HTTP的底层socket完成后续操作

message底层被切分成多个frame 帧传输。

编程是只需要操作message不需要关心frame

框架底层完成TCP网络I/O,WebSocker协议解析,开发者不需要关心。

package main

import "net/http"

func wsHandle(writer http.ResponseWriter, request *http.Request) {
      writer.Write([]byte("hello world"))
}

func main() {
    http.HandleFunc("/ws", wsHandle)
    http.ListenAndServe(":8888",nil)
    
}

服务请求参数

Request URL: http://localhost:8888/ws
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:8888

websocket的服务器端代码

package main

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

func wsHandle(writer http.ResponseWriter, request *http.Request) {
    upgrader:= websocket.Upgrader{CheckOrigin: func(r *http.Request) bool {
          return true
      }}
    con,err := upgrader.Upgrade(writer,request,nil)
    defer con.Close()
    if err!=nil {
        writer.Write([]byte(err.Error()))
    }
    for{
        _, p, err := con.ReadMessage()
        if err!= nil {
            writer.Write([]byte(err.Error()))
            break
        }
        fmt.Println("client message "+string(p))
        con.WriteMessage(websocket.TextMessage,[]byte(time.Now().String()))
    }
}

func main() {
    http.HandleFunc("/ws", wsHandle)
    http.ListenAndServe(":8888",nil)
    
}
Golang——WebSocket

image.png

websocket 客户端代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script>
        window.addEventListener("load", function(evt) {
            var output = document.getElementById("output");
            var input = document.getElementById("input");
            var ws;
            var print = function(message) {
                var d = document.createElement("div");
                d.innerHTML = message;
                output.appendChild(d);
            };
            document.getElementById("open").onclick = function(evt) {
                if (ws) {
                    return false;
                }
                ws = new WebSocket("ws://localhost:8888/ws");
                ws.onopen = function(evt) {
                    print("连接websocket");
                }
                ws.onclose = function(evt) {
                    print("CLOSE");
                    ws = null;
                }
                ws.onmessage = function(evt) {
                    print("收到消息: " + evt.data);
                }
                ws.onerror = function(evt) {
                    print("ERROR: " + evt.data);
                }
                return false;
            };
            document.getElementById("send").onclick = function(evt) {
                if (!ws) {
                    return false;
                }
                print("发送消息: " + input.value);
                ws.send(input.value);
                return false;
            };
            document.getElementById("close").onclick = function(evt) {
                if (!ws) {
                    return false;
                }
                ws.close();
                return false;
            };
        });
    </script>
</head>
<body>
<table>
    <tr><td valign="top" width="50%">
            <p>点击启动按钮创建websocket连接<br/>
               点击发送按钮可以发送任意消息给服务器<br/>
               点击关闭按钮断开websocket连接
            </p>
            <form>
                <button id="open">启动</button>
                <button id="close">关闭</button>
                <input id="input" type="text" value="Hello golang!">
                <button id="send">发送</button>
            </form>
        </td><td valign="top" width="50%">
            <div id="output"></div>
        </td></tr></table>
</body>
</html>

运行client.html,效果如下

Golang——WebSocket

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python the Hard Way

Learn Python the Hard Way

Zed Shaw / Example Product Manufacturer / 2011

This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换