Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/moteus/lua-pegasus-websocket

WebSocket plugin for Pegasus http server
https://github.com/moteus/lua-pegasus-websocket

Last synced: 13 days ago
JSON representation

WebSocket plugin for Pegasus http server

Awesome Lists containing this project

README

        

# lua-pegasus-websocket
WebSocket plugin for [Pegasus](http://evandrolg.github.io/pegasus.lua) http server

### Limitations

This plugin just do upgrade and returns raw socket. So user have to use
some other library to handle actuall WebSocket protocol.

### Usage

```Lua
local Pegasus = require "pegasus"
local WebSocket = require "pegasus.plugins.websocket"

local server = Pegasus:new{ plugins = {
WebSocket:new{
protocols = { 'echo' }
}
} }

server:start(function(request, response)
-- have to receive full request
request:receiveBody()

if request:is_upgrade() then
local protocol, client = response:upgrade()
if client then
-- because we have only one plugin its can be only websocket
assert(protocol == 'websocket')
-- detect choosen sub protocol
protocol = response.headers['Sec-WebSocket-Protocol']
print("Upgrade to websocket:", protocol)
print(' - Socket:', client)
-- here we need wrap `client` to new WebSocket socket
....
return
end
print('Can not do upgrade')
end

end)
```