Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a web socket with Lua scripting language?

Tags:

websocket

lua

As a beginner, I want to open a web socket with Lua on a Linux-based server. This server should allow Android client to connect to it. Can you please give me some example code of opening web socket with Lua?

like image 327
Jung Hur Avatar asked Jun 17 '13 09:06

Jung Hur


1 Answers

You already asked the same question two weeks ago that was answered: LUA Script - web socket communication. Have you looked at lua-websockets? What have you tried? What's not working?

Examples from the websockets module I referenced earlier:

-- create client:

local websocket = require'websocket'
local client = websocket.client.copas({timeout=2})

-- connect to the server:

local ok,err = client:connect('ws://localhost:12345','echo')
if not ok then
   print('could not connect',err)
end

-- send data:

local ok = client:send('hello')
if ok then
   print('msg sent')
else
   print('connection closed')
end

-- receive data:

local message,opcode = client:receive()
if message then
   print('msg',message,opcode)
else
   print('connection closed')
end

-- close connection:

local close_was_clean,close_code,close_reason = client:close(4001,'lost interest')

Have you tried them? Ran into issues?

like image 135
Paul Kulchenko Avatar answered Sep 22 '22 11:09

Paul Kulchenko