Git Product home page Git Product logo

tcp-heartbeat's Introduction

TCP心跳

对于socket长连接,只要socket建立后不关闭即是长连接,要知道tcp的连接数是有限的,所以关闭是必须的,关键是什么时候关怎么关。

那到底是关客户端socket,还是关服务端socket呢?一般客户端会因为各种原因掉线,所以关服务端是更好的选择。我们可以设定一段时间,当超过这段时间客户端没有任何操作时就去关闭它。

我们让客户端定时(比如每隔5秒)地向服务端发送心跳包,之所以叫心跳包是因为它就像你心脏一样有节律的跳动。为了与普通消息区别开可以附加上一个type属性。

服务端需要维护一个全局心跳列表,简单起见我这里使用map来存,map的key存服务端socket,map的value存最后一次收到来自客户端心跳包的时间戳。服务端线程在每次收到心跳包后就去map里更新一下时间戳。

服务端还需开启另外一个线程,该线程会去定时(比如每隔1分钟)扫描这个全局map,把扫描到的超时(当前时间 - 最后更新时间 > 设定阈值)的socket关闭。

Run

go run server.go
go run client.go -n 客户端名字A
go run client.go -n 客户端名字B

hi,amigo!

Related Topic

tcp-heartbeat's People

Contributors

guobinqiu avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.