All Projects → CandyMi → skynet-lua-websocket

CandyMi / skynet-lua-websocket

Licence: MIT License
Add a lua websocket library for the skynet game server framework.

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to skynet-lua-websocket

channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-13.04%)
Mutual labels:  websocket-server
apollo-chat-graphql-server
Apollo Chat is a Chat Service build on GraphQL Apollo with Subscriptions
Stars: ✭ 13 (-43.48%)
Mutual labels:  websocket-server
node-jsonrpc2
JSON-RPC 2.0 server and client library, with HTTP (with Websocket support) and TCP endpoints
Stars: ✭ 103 (+347.83%)
Mutual labels:  websocket-server
skynet
Use Sia Skynet and SkyDB in your Dart and Flutter projects (Decentralized CDN and database)
Stars: ✭ 25 (+8.7%)
Mutual labels:  skynet
simple-websocket-server
A simple WebSocket server
Stars: ✭ 26 (+13.04%)
Mutual labels:  websocket-server
freeioe
FreeIOE is a framework for building IOE (Internet Of Everything) edge-computing gateway 开源的边缘计算网关框架. 讨论群: 291292378
Stars: ✭ 77 (+234.78%)
Mutual labels:  skynet
tornado-websocket-client-example
Websocket client application example built on top of Tornado.
Stars: ✭ 34 (+47.83%)
Mutual labels:  websocket-server
LazWebsockets
Websocket Server and Client Library written in Lazarus
Stars: ✭ 51 (+121.74%)
Mutual labels:  websocket-server
JINS-MEME-Python-WebSocketServer-Sample
JINS MEME(2021年モデル)のJINS MEME LoggerをPythonのWebSocketサーバーで受信するサンプル
Stars: ✭ 19 (-17.39%)
Mutual labels:  websocket-server
cs
开箱即用的基于命令的消息处理框架,让 websocket 和 tcp 开发就像 http 那样简单
Stars: ✭ 19 (-17.39%)
Mutual labels:  websocket-server
remoting
Jetlang Remoting - asynchronous distributed messaging
Stars: ✭ 27 (+17.39%)
Mutual labels:  websocket-server
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (+191.3%)
Mutual labels:  websocket-server
WebSocket Server
A WebSocket server module for PureBasic
Stars: ✭ 18 (-21.74%)
Mutual labels:  websocket-server
sandstone-edition
Build a Real-time RestApi.
Stars: ✭ 17 (-26.09%)
Mutual labels:  websocket-server
reactive-streams-for-java-developers
No description or website provided.
Stars: ✭ 16 (-30.43%)
Mutual labels:  websocket-server
customer-service
客服IM服务端,基于t-io
Stars: ✭ 30 (+30.43%)
Mutual labels:  websocket-server
skynet-cocos-creator
skynet cocoscreator puremvc
Stars: ✭ 44 (+91.3%)
Mutual labels:  skynet
rony
Fast and Scalable RPC Framework
Stars: ✭ 41 (+78.26%)
Mutual labels:  websocket-server
sywebsocket
WebSocket Server and Client
Stars: ✭ 16 (-30.43%)
Mutual labels:  websocket-server
text
An experiment with WebSockets and the human condition.
Stars: ✭ 51 (+121.74%)
Mutual labels:  websocket-server

skynet-lua-websocket

Add a lua websocket library for the skynet game server framework.

Most of mobile apps use Websocket for communication, e.g: Wechat、H5、Web Service push.

A Simplify Websocket Server Example

The following usage examples will show you how to use skynet-websocket:

-- main.lua for skynet start script.
local skynet = require "skynet"
local socket = require "skynet.socket"
local wsserver = require "websocket.server"
local ws = require "ws"

skynet.start(function()
  local server = socket.listen("0.0.0.0", 8000, 128)
  socket.start(server, function(id, ipaddr)
    local wss = wsserver:new {
      fd = id,
      cls = ws,
      nodelay = true,
    }
    return wss:start()
  end)
end)

We launch a socket server(listen 0.0.0.0:8000) And All client connections will be received via the socket.start callback.

Create a Websocket.server object for each connection in the callback function and manage all events using the Websocket.server object.

The websocket.server object requires a parameter of lua.websocket.class, the sample file is here.

-- ws.lua is a lua.websocket.class sample file.
local skynet = require "skynet"
local class = require "class"

local ws = class("ws")

function ws:ctor (opt)
  self.ws = opt.ws             -- websocket对象
  self.headers = opt.headers   -- http headers
  self.send_masked = false     -- 掩码(默认为false, 不建议修改或者使用)
  self.max_payload_len = 65535 -- 最大有效载荷长度(默认为65535, 不建议修改或者使用)
end

function ws:on_open ()
  self.state = true
  self.count = 1
  skynet.error("客户端已连接")
end

function ws:on_message (msg, type)
  -- 客户端消息
  skynet.error("客户端发送消息:", msg, type)
  self.ws:send(msg, type == 'binary')
  -- self.ws:close()
end

function ws:on_error (msg)
  -- 错误消息
  skynet.error("错误的消息:", msg)
end

function ws:on_close (msg)
  -- 清理数据
  skynet.error("客户端断开了连接:", msg)
end

return ws

To simplify the use of Websocket.server, we have defined four callback methods(must) for everyday use.

  • on_open, When the connection has been established.

  • on_message, When the server receives the message from the client.

  • on_error, When the protocol is wrong or other exceptions.

  • on_close, websocket.client or websocket.server actively disconnects(Usually used to clean up data).

Open Chrome browser and install wsc.crx plugin to test it. Start enjoying it. (passcode: cgwr)

TODO

write websocket.client. :)

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].