8

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?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Jung Hur
  • 109
  • 1
  • 1
  • 7
  • this question had been asked before in [SO](http://stackoverflow.com/questions/16897557/lua-script-web-socket-communication) – mrz Jun 17 '13 at 09:44
  • @mrz yae, but there are too many codes that i don't know which one is it..so i want just a sample code to open websocket! that's all! – Jung Hur Jun 17 '13 at 09:52
  • @mrz and can you please tell me the difference between LUA-ev and copas? – Jung Hur Jun 17 '13 at 09:55
  • Here usually people don't do your task, instead they help you to overcome your problems, you have to try by yourself. Try example folder in [this](https://github.com/lipp/lua-websockets) project and let us know what you couldnt achieve. – mrz Jun 17 '13 at 09:56
  • What's the difference between LUA-ev and copas? – Jung Hur Jun 17 '13 at 09:59
  • I haven't use Copas, but it seems they are two different project with "almost" the same objective. Read [this](http://stackoverflow.com/questions/5047951/how-can-a-luasocket-server-handle-several-requests-simultaneously). Hope it helps. – mrz Jun 17 '13 at 10:00
  • 1
    Thanks!!I will try it and...maybe ask you later ! – Jung Hur Jun 17 '13 at 10:01
  • 1
    @JungHur: If you're a beginner with a language, you aren't ready for complex things like "open a web socket" and such. – Nicol Bolas Jun 17 '13 at 15:21
  • This should also be mentioned http://www.websocketserver.de/ – Frederik Spang Jun 17 '13 at 15:26
  • 1
    @NicolBolas thank you for your advice but I know it's quite challenge using like "websocket" as you mentioned. But, for some reasons, I have to do. I must implement it. And that's why I requested advice from here. – Jung Hur Jun 17 '13 at 16:12
  • @FrederikSpang Thanks! this will be really useful to me. – Jung Hur Jun 17 '13 at 16:13

1 Answers1

12

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?

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thank you for your previous answer, but not this one. I have looked at lua-websockets. And as you know(if you've checked it), there are a bunch of code. And I looked the file 'server_ev' because ev is installed in the machine what I am using. But there are full of other things that i don't know. What I want is just a few lines of opening a websocket and closing of sample code. It is same that someone asks where to buy a fruit, and someone answers "IN PARIS", not the exact location. – Jung Hur Jun 17 '13 at 16:10
  • If you are looking for the server code, it's in one [short example](https://github.com/lipp/lua-websockets/blob/master/examples/echo-server-copas.lua) (most of it are comments). If you are looking for the client side, it's also in the documentation; I added code snippets to the answer. – Paul Kulchenko Jun 17 '13 at 18:05
  • Thank you for your answer. This answer is perfect answer for the client as I wanted. But, what I need is just on the server side. Because android device will be the client. By the way, actually, I don't think I'm using "copas", but "ev" and your answer is based on copas..maybe they are almost same..right? – Jung Hur Jun 17 '13 at 21:06