Golang(10)Web Service - Web Socket
8.1 Socket
2 types of socket, TCP Socket, UDP Socket. IP, port, TCP/UDP.
8.2 WebSocket
Implement the Push Notification to Browsers
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/08.2.md
iOS and Android can support web socket too.
http://www.elabs.se/blog/66-using-websockets-in-native-ios-and-android-apps
Install the the go.net package
>go get code.google.com/p/go.net/websocket
Error Message:
go: missing Mercurial command. See http://golang.org/s/gogetcmd package code.google.com/p/go.net/websocket: exec: "hg": executable file not found in $PATH
Solution:
https://code.google.com/p/go-wiki/wiki/GoGetTools
>sudo port install mercurial
Error Message:
Error: org.macports.extract for port py27-docutils returned: command execution failed Error: Failed to install py27-docutils
:error:extract Failed to install py27-docutils :debug:extract couldn't open "/System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/tclIndex": no such file or directory while executing
Solution:
>sudo port clean
>sudo port selfupdate
>sudo port install py27-docutils
>sudo port install mercurial
>>go get code.google.com/p/go.net/websocket
Follow the document, but I get these error when I try to connect to web socket server.
Error Message:
failed: Error during WebSocket handshake: Unexpected response code: 403
Solution:
Make them the same domain name.
Go Server Side
package main
import (
"code.google.com/p/go.net/websocket"
"fmt"
"log"
"net/http"
)
func Echo(ws *websocket.Conn) {
var err error
for {
var reply string
if err = websocket.Message.Receive(ws, &reply); err != nil {
fmt.Println("Can't receive")
break
}
fmt.Println("Received back from client: " + reply)
msg := "Received: " + reply
fmt.Println("Sending to client: " + msg)
if err = websocket.Message.Send(ws, msg); err != nil {
fmt.Println("Can't send")
break
}
}
}
func main() {
http.Handle("/", http.FileServer(http.Dir(".")))
http.Handle("/socket", websocket.Handler(Echo))
if err := http.ListenAndServe("127.0.0.1:8008", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
HTML Client Side
<html>
<head></head>
<body>
<script type="text/javascript">
var sock = null;
var wsuri = "ws://127.0.0.1:8008/socket";
window.onload = function() {
console.log("onload");
sock = new WebSocket(wsuri);
sock.onopen = function() {
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);
}
};
function send() {
var msg = document.getElementById('message').value;
sock.send(msg);
};
</script>
<h1>WebSocket Echo Test</h1>
<form>
<p>
Message: <input id="message" type="text" value="Hello, world!">
</p>
</form>
<button onclick="send();">Send Message</button>
</body>
</html>
8.3 REST
…soon...
8.4 RCP (Remote Procedure Call Protocol)
…snip...
References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/08.0.md
web socket push notification
http://html5hacks.com/blog/2013/04/21/push-notifications-to-the-browser-with-server-sent-events/
http://stackoverflow.com/questions/15884480/static-html-page-created-the-websocket-connection-golang-server-directly
- 浏览: 2552576 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 574NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 371Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
ftp+web用户管理界面 安装博客地址:https://blog.csdn.net/zhanremo3062/article/details/123083547?spm=1001.2014.3001.5502 创作不易,感谢大家以来的支持!
在Golang中实现AES-256-CBC(Advanced Encryption Standard with a 256-bit key in Cipher Block Chaining mode)是一种常见的加密方法,用于确保数据的安全性。然而,Golang的标准库`crypto/cipher`在处理密钥时...
go-socket.io是socket.io在golang中的一个实现,一个实时应用程序框架
Golang标准化课件--语法基础阶段 .ctb
docker golang 基础包1.20-alpine
golang-swaggerui-example Golang-swaggerui-example是一个示例存储库,用于在您的Golang项目中使用SwaggerUI设置API文档。 详细说明可在。 API互动 可以通过访问localhost:8080 / swaggerui /访问SwaggerUI,有关...
golang protoc-gen-go 编译的工具, 配合 protoc 可以编译proto文件,比如: protoc message.proto --plugin=./protoc-gen-go.exe --go_out=E:/game/message
golang-html5-sse-example, HTML5服务器发送的事件 Golang HTML5 SSE示例这是一个简单的例子,说明如何使用发送事件发送事件。 从服务器的角度来看,SSE几乎与长轮询相同。 客户端发出一个建立TCP连接的获取请求。 ...
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
Golang,由于其简洁的语法和高效的性能,也成为了现代Web开发的热门选择。将两者结合,我们可以构建高效且可扩展的应用程序。 首先,我们需要了解Redis的基础知识。Redis支持多种数据结构,如字符串、哈希、列表、...
Go-golang版的dsinff-webspy是一款强大的网络监控工具,它利用Go语言的高效性能和丰富的网络编程库,能够在本地浏览器中实时显示被嗅探的服务器发送的所有HTTP请求。这款工具对于开发者、网络安全专家以及系统管理员...
golang-stats-api-handler, Golang cpu,内存,gc等信息api处理程序 golang-stats-api-handlerGolang cpu,内存,gc等信息api处理程序。 安装go get github.com/fukata/golang-stats-api-handler示
Golang的Web API演示 # build images $ docker-compose build # setup db schema $ docker-compose run --rm db_migration goose create AddUsersTable sql $ docker-compose run --rm db_migration goose up $ ...
开源项目-thewhitetulip-web-dev-golang-anti-textbook.zip,Added a chapter on Building API in my book, would like constructive reviews
一个纯粹而苗条的Golang SSH-Jumper-bastion-web应用程序。 一个二进制文件将所有SSH-Fortress Go版本都设置为1.12以上。Windows10需要GCC才能与SQLite(CGO)数据库1一起运行。 通过通过SSH-Fortress服务器公开SSH...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
谢大大编写了build-web-application-with-golang ,托管在github上,只有md版的,我个人把它编译成了chm版,共享给大家,又名《Go语言Web编程》,希望各位喜欢
《构建Web应用 with Go语言》是一本开源书籍,其源代码存放在GitHub上,并被整理成"build-web-application-with-golang-master.zip"的压缩包文件。这个压缩包包含了一个完整的Go语言开发Web应用的教程项目,是学习Go...