Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
# ws.nvim [WIP]

A Neovim websocket library

## Installation

Install using packer.

```lua
-- ws.nvim
use {
'rohanorton/ws.nvim',
requires = {
'nvim-lua/plenary.nvim'
}
}
```

## Usage

```lua
local async = require'plenary.async'
local channel = async.control.channel
local WebSocketClient = require 'ws.websocket_client'

local M = {}

local test_ws = function()
local tx, rx = channel.oneshot()

local has_received_data = false
local _msg = ""

-- Setup client to connect to websocket echo server.
local ws = WebSocketClient("ws://websocket-echo.com/")

-- Setup Handlers.
ws.on_open(function()
ws.send("hello")
end)

ws.on_close(function()
print("Websocket closed " .. _msg)
assert(has_received_data == true, "Websocket closed before any data received.")
tx(true)
end)

ws.on_error(function(err)
tx(err)
end)

ws.on_message(function(msg)
has_received_data = true
_msg = msg:to_string()
assert(_msg == "hello", "hello")
-- Close server on successful message
ws:close()
end)

-- Connect to server.
ws.connect()
end

M.setup = function()
vim.api.nvim_create_user_command('Wsexample', function()
print('Hello from Wsexample')
test_ws()
end, { nargs = '?'}
)
end

return M
```