Coder Social home page Coder Social logo

websocket_by_libuv's Introduction

websocket_by_libuv

基于libuv实现websocket解析,握手等

libuv在链接https://github.com/libuv/libuv

websocket是HTML5上的一个新协议,实现浏览器与服务器全双工通信。

握手时要借助HTTP协议后面就没它什么事了, 握手告诉服务器协议要升级为websocket还是子协议,是否支持,一般格式如下:

GET /chat HTTP/1.1

Host: server.example.com

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==

Sec-WebSocket-Protocol: chat, superchat

Sec-WebSocket-Version: 13

Origin: http://example.com

通过返回码可以知道是否支持,成功是101,正确格式如下: HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=

Sec-WebSocket-Protocol: chat

代码里用的子协议是自定义的zyx_protocol, Sec-WebSocket-Key是客户端随机生成的Base64 encode值, 服务端采用SHA-1处理返回Sec-WebSocket-Accept。

长链接一般都要保持心跳,协议里建议可以用ping,pong来完成 代码中也是这样用,根据nodejs里的websocket,服务端连上后要每隔一段时间发个ping,客户端有回复pong就保持连接,不然就超时断开。 客户端已实现自动回复pong,服务端要设置定时器发ping,还少了检测超时机制。

代码是基于libuv上来实现的,nodejs底层就是用libuv。简单实现了客户端及服务端的交流。

websocket_by_libuv's People

Contributors

zyx4843 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.