GithubHelp home page GithubHelp logo

websocket's Introduction

websocket

安装

go get github.com/wangsir0624/websocket

用法

基本用法

package main

import (
	"fmt"
	"websocket"
)

func main() {
	//监听IP和端口
	server := websocket.Listen("127.0.0.1", 11111)
	
	//注册创建连接回调事件
	server.On("connection", func(conn *websocket.Conn) {
		fmt.Printf("accept the connection from %s\r\n", conn.RemoteAddr())
	})
	
	//注册错误回调函数
	server.On("error", func(conn *websocket.Conn) {
		fmt.Println(conn.GetErr())
	})
	
	//注册接受消息回调函数
	server.On("message", func(conn *websocket.Conn) {
		fmt.Printf("receive message from %s: %s\r\n", conn.RemoteAddr(), conn.GetData())

		conn.Send(conn.GetData())
	})
	
	//注册关闭连接回调函数
	server.On("close", func(conn *websocket.Conn) {
		fmt.Printf("the connection from %s was closed\r\n", conn.RemoteAddr())
	})
	
	//运行服务器
	server.Run()
}

广播

对所有连接广播消息

server.On("message", func(conn *websocket.Conn) {
		fmt.Printf("receive message from %s: %s\r\n", conn.RemoteAddr(), conn.GetData())

		conn.GetServer().Broadcast(conn.GetData())
})

对其他连接广播消息

server.On("message", func(conn *websocket.Conn) {
		fmt.Printf("receive message from %s: %s\r\n", conn.RemoteAddr(), conn.GetData())

		conn.GetServer().BroadcastToOthers(conn.GetData(), conn)

		//方法二
		conn.GetServer().BroadcastOnly(conn.GetData(), func(c *websocket.Conn) bool {
			return c.RemoteAddr().String() != conn.RemoteAddr().String()
		})

		//方法三
		conn.GetServer().BroadcastExcept(conn.GetData(), func(c *websocket.Conn) bool {
			return c.RemoteAddr().String() == conn.RemoteAddr().String()
		})
})

服务器状态监控

本服务器采用HTTP协议的方式来监控服务器状态,监听端口为websocket端口加1,比如上面的例子中websocket端口为11111,那么http端口即为11112,在浏览器中输入http://127.0.0.1:11112,就可以监听服务器运行状态了

websocket's People

Contributors

wangsir0624 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gladiopeace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.