lipp / lua-websockets

Websockets for Lua.
http://lipp.github.com/lua-websockets/
MIT License
396 stars 113 forks source link

pure-lua version interface #10

Closed stuta closed 11 years ago

stuta commented 11 years ago

I also need 'as thin as possible' version and this pure-lua version is great. I also need to integrate websocket to existing http server event loop using Luajit and i think I can do it using copas.step().

I have also made a simple http+websocket server using previous libwebsockets 'on_http' event. Is is possible to get 'other-than-websocket' event callback into test-server-copas.lua example?

Maybe you should focus only on pure-lua version and bypass all problems related to other libraries. Luajit is fast.

lipp commented 11 years ago

I guess I have the same setup :) Using luajit + orbit (webserver) + lua-websockets. I run the webserver and the websocket stuff in different processes though. Using one process and copas.step to spin the wheel should work. Having different processes implies having different ports for http and websockets, but I dont consider this a problem, since you are allowed to open websockets from browser to any port. I will add more doc and maybe pimp the example to your scenario. I want to roll out pure-lua branch asap. Takes maybe 2-3 weeks I guess.

stuta commented 11 years ago

I think it is better to get some working version of pure-lua. Performance is good now and optimizations should be made after it works. I'm still investigating what is the fastest simple tcp server, there seems to be lots of different options.

Current master gives an error: lua/copas.lua:156: bad argument #1 to 'send' (string expected, got nil).

lipp commented 11 years ago

Can you provide me the code causing the send problem? The tests all pass....

stuta commented 11 years ago

Previous one was my "too tired" error, I need now 3 servers to test this and web server was missing. Now to to real problem:

Tis is a simple test task app using plain json text. When I first time start server this works, but when I send some other message like delete then this same message fails and it will not work until restart. Trouble is in websocket answer. Last week this failed every time.

I had similar-sounding problems with my raw-tcp messages when the message did not fit in single tcp packet. I had to wait for the end of message before sending it forward.

my Lua code: local answer = callJsonRpc(data) -- raw-tcp call to another server if answer then print("json-rpc broadcast: " ..jsonRpcMsgCount.. ". " ..answer) ws:broadcast(answer) else ws:broadcast("error: server call returned null") end

the error: lua/websocket/sync.lua:68: Websocket client send failed:closed

answer: json-rpc broadcast: 5. {"kind":"tasks#task","etag":"tasks#task","nextPageToken":"tasks#task","items":[{"kind":"tasks#task","id":"20120913.004253.21001.do zzy00026bb6849cc0b","etag":"20120913.004253.21001.do zzy00026bb6849cc0b","title":"manageri.fi kuntoon","updated":"","selfLink":"","parent":"","position":"20120913.004253.21001.do zzy00026bb6849cc0b","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"},{"kind":"tasks#task","id":"20120913.013719.ck01k.do zzy00026bb6849cc0b","etag":"20120913.013719.ck01k.do zzy00026bb6849cc0b","title":"laskinesitteen uusiminen Excel kuvalla","updated":"","selfLink":"","parent":"","position":"20120913.013719.ck01k.do zzy00026bb6849cc0b","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"},{"kind":"tasks#task","id":"20120913.013719.as01j.do zzy00026bb6849cc0b","etag":"20120913.013719.as01j.do zzy00026bb6849cc0b","title":"-ei-rahoitusmalli-esite (ei vuokrausta esitteeseen ennen kun vuokraus on mietitt","updated":"","selfLink":"","parent":"","position":"20120913.013719.as01j.do zzy00026bb6849cc0b","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"},{"kind":"tasks#task","id":"20120913.013713.p4017.do zzy00026bb6849cc0b","etag":"20120913.013713.p4017.do zzy00026bb6849cc0b","title":"integrointi-esite","updated":"","selfLink":"","parent":"","position":"20120913.013713.p4017.do zzy00026bb6849cc0b","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"},{"kind":"tasks#task","id":"20120911.143031.9i00z.do zzy0001d0fbe9b7a0d","etag":"20120911.143031.9i00z.do zzy0001d0fbe9b7a0d","title":"wordpress kuntoon","updated":"","selfLink":"","parent":"","position":"20120911.143031.9i00z.do zzy0001d0fbe9b7a0d","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"},{"kind":"tasks#task","id":"20120913.052730.9i01g.do zzy00026bb6849cc0b","etag":"20120913.052730.9i01g.do zzy00026bb6849cc0b","title":"*prosessi messuista prosesikansioon sitä mukaan kun tehdään","updated":"","selfLink":"","parent":"","position":"20120913.052730.9i01g.do zzy00026bb6849cc0b","notes":"","status":"needsAction","due":"","completed":"","deleted":false,"hidden":false,"links":"[]"}],"result":"tasks#list ok","id":"tasks-1-38550FB5-43C0-4036-ABBE-92AA126D7091"}

lua/websocket/sync.lua:68: Websocket client send failed:closed thread: 0x000b9d48 tcp{client}: 0xf7c80

stuta commented 11 years ago

I just compiled and installed Luajit 2.0.1 and above error does not show any more (after short tests).

stuta commented 11 years ago

With more tests I get errors after refreshing the page sources and sometimes just randomly. After first error I must shut down lua code and restart to get rid of errors. These errors happen also with plain Lua 5.1.5. Maybe some code you did within 2 days fixed part of the problem, it is not a Luajit issue.

chrome error: lua/websocket/sync.lua:69: Websocket client send failed:closed

firefox error: lua/websocket/sync.lua:64: Websocket not OPEN

lipp commented 11 years ago

Sorry, just became father ... I will have a look ... Btw: are you using copas or ev server? Could you narrow the source of error please? I guess closing the browser is not handled correctly in all cases....

lipp commented 11 years ago

I improved / fixed the detection of connection close. broadcasts after unexpected close could crash the server... Could you verify, if this fixes your problems, please?

stuta commented 11 years ago

Quick test with Chrome and Safari, could not load the page:

lua/copas.lua:156: bad argument #1 to 'send' (string expected, got nil) thread: 0x00063570 tcp{client}: 0xdf0c0 Websocket unhandled error Websocket server Handshake failed due to copas receive err:closed


I'm creating my own webserver and I'm not going to use Copas. All I need is 'as simple as possible' library to handle websockets with my tcp server.

https://github.com/stuta/Luajit-Tcp-Server 105 000 roundtrip messages / second with tcp between 2 applications 4,8 million roundtrip messages / second with shared memory between 2 applications This project is in very early stage, but performance is great.

lipp commented 11 years ago

could you provide me your test source code, please? How does your Tcp-Server manage asynchrony / deal with multiple clients? Do you employ some kind of event loop (or select / poll bindings?)?

stuta commented 11 years ago

get version: git clone https://github.com/stuta/Luajit-Tcp-Server.git

Info and instructions are on frontpage: https://github.com/stuta/Luajit-Tcp-Server

I will add worker processes with system threads or applications using shared memory. Currently it is just a test "how fast can tcp server be"? You can debug AppServer.lua with ZeroBraneStudio. Code is very simple.

lipp commented 11 years ago

i'd prefer a "slim as possible" test which reproduces the error. This would save me a lot of time. OSX or Linux environment would be very nice! Out of curiosity: What is your TCP-Server supposed to do (TCP-Server is a quite generic name)?

btw: I cannot recommend using Lua with multiple OS threads. You will lose many advantages of Lua. Also you need synchronization stuff etc. beware of races, deadlocks. I'd recommend using lua-ev or another event-loop instead. I am doing a lot of "server-style" programming with Lua and this is really powerful and fun. I have a strong background in writing this kind of thing in c/c++ using multiple os threads and it is very hard to do it correctly and it is no fun at all. If performance is your concern; single threaded approaches seem to perform better than multi-threaded pendants (that is one reason why node.js is so successful). You may want to have a look at http://luvit.io/ which is node.js in Lua.

lipp commented 11 years ago

Hi! I'd like to close this "thread" here, since the issue title is a bit misleading. If you have any issues / bugs, it'll be really nice if you can open a new issue.