设为首页 加入收藏

TOP

Go语言【项目】 websocket消息服务(一)
2019-09-14 00:50:51 】 浏览:125
Tags:语言 项目 websocket 消息 服务

websocket消息服务

 

目的:搭建websocket服务,用浏览器与服务进行消息交互(写的第一个Go程序)

 

代码目录结构:

 

前端html页面:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <script>
 6         window.addEventListener("load", function(evt) {
 7             var output = document.getElementById("output");
 8             var input = document.getElementById("input");
 9             var ws;
10             var print = function(message) {
11                 var d = document.createElement("div");
12                 d.innerHTML = message;
13                 output.appendChild(d);
14             };
15             document.getElementById("open").onclick = function(evt) {
16                 if (ws) {
17                     return false;
18                 }
19                 ws = new WebSocket("ws://127.0.0.1:7777/ws");
20                 ws.onopen = function(evt) {
21                     print("OPEN");
22                 }
23                 ws.onclose = function(evt) {
24                     print("CLOSE");
25                     ws = null;
26                 }
27                 ws.onmessage = function(evt) {
28                     print("RESPONSE: " + evt.data);
29                 }
30                 ws.onerror = function(evt) {
31                     print("ERROR: " + evt.data);
32                 }
33                 return false;
34             };
35             document.getElementById("send").onclick = function(evt) {
36                 if (!ws) {
37                     return false;
38                 }
39                 print("SEND: " + input.value);
40                 ws.send(input.value);
41                 return false;
42             };
43             document.getElementById("close").onclick = function(evt) {
44                 if (!ws) {
45                     return false;
46                 }
47                 ws.close();
48                 return false;
49             };
50         });
51     </script>
52 </head>
53 <body>
54 <table>
55     <tr><td valign="top" width="50%">
56         <p>Click "Open" to create a connection to the server,
57             "Send" to send a message to the server and "Close" to close the connection.
58             You can change the message and send multiple times.
59         </p>
60             <form>
61                 <button id="open">Open</button>
62                 <button id="close">Close</button>
63             <input id="input" type="text" value="Hello world!">
64             <button id="send">Send</button>
65             </form>
66     </td><td valign="top" width="50%">
67         <div id="output"></div>
68     </td></tr></table>
69 </body>
70 </html>
client.html

 

server.go代码:

package main

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

// http升级websocket协议的配置
var wsUpgrader = websocket.Upgrader{
	// 允许跨域CORS
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

// 消息处理
func wsHandler(resp http.ResponseWriter, req *http.Request) {
	wsSocket, err := wsUpgrader.Upgrade(resp, req, nil)
	if err != nil {
		return
	}
	wsConn := go_websocket.WsConnectionInit(wsSocket)
	wsConn.Run()

	for {
		wsmsg, err := wsConn.ReadMessage()
		if err != nil {
			goto error
		}
		err = wsConn.WriteMessage(wsmsg)
		if err != nil {
			goto error
		}
	}
error:
	fmt.Println("websocket is closed")
	return
}

func main() {
	fmt.Println("websocket start")
	http.HandleFunc("/ws", wsHandler)
	http.Liste
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Golang读取并修改非主流配置文件 下一篇Golang解析、验证、修改URL之Host..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目