kernelsauce / turbo

Turbo is a framework built for LuaJIT 2 to simplify the task of building fast and scalable network applications. It uses a event-driven, non-blocking, no thread design to deliver excellent performance and minimal footprint to high-load applications while also providing excellent support for embedded uses.
http://turbo.readthedocs.io/
Apache License 2.0
528 stars 84 forks source link

Websocket client example is broken #246

Closed cinterloper closed 8 years ago

cinterloper commented 8 years ago

cat stdln.lua

_G.TURBO_SSL = true -- SSL must be enabled for WebSocket support!
local turbo = require("turbo")

function startev() 
  turbo.ioloop.instance():start()
end

function getws(addr)
  turbo.ioloop.instance():add_callback(function()
    turbo.websocket.WebSocketClient(addr, {
        on_headers = function(self, headers)
            print(headers)
            -- Review headers recieved from the WebSocket server.
            -- You can e.g drop the request if the response headers
            -- are not satisfactory with self:close().
        end,
        modify_headers = function(self, headers)
            -- Modify outgoing headers before they are sent.
            -- headers parameter are a instance of httputil.HTTPHeader.
        end,
        on_connect = function(self)
           print("connected")
            -- Called when the client has successfully opened a WebSocket
            -- connection to the server.
            -- When the connection is established you can write a message:
            self:write_message("Hello World!")
        end,
        on_message = function(self, msg)
            -- Print the incoming message.
            print(msg)
            self:close()
        end,
        on_close = function(self)
            -- I am called when connection is closed. Both gracefully and
            -- not gracefully.
        end,
        on_error = function(self, code, reason)
            -- I am called whenever there is a error with the WebSocket.
            -- code are defined in ``turbo.websocket.errors``. reason are
            -- a string representation of the same error.
        end
    })
  end):start()
end

function redirections()
    repeat
        a = io.read()
        if a ~= nil then print("xx" .. a .. "xx") end
    until a == nil
end

getws("ws://echo.websocket.org")
$ luajit stdln.lua  
[W 2016/01/05 05:51:50] [async.lua] GET echo.websocket.org:80/ => 400 Bad Request 208ms
[E 2016/01/05 05:51:50] Excpected 101, was 400, can not upgrade.
yurayko commented 8 years ago

Work for me. Debian 8.1

yuray@yuray:/tmp$ luajit t.lua 
[W 2016/01/13 13:58:39] [async.lua] GET echo.websocket.org:80/ => 101 Switching Protocols 452ms
instance of class HTTPParser
[S 2016/01/13 13:58:39] [websocket.lua] WebSocketClient connection open ws://echo.websocket.org
connected
Hello World!
[S 2016/01/13 13:58:39] [websocket.lua] WebSocketClient closed ws://echo.websocket.org 595ms
rjpcomputing commented 8 years ago

Works for me too. Debian 8.2

$ luajit websocket_example.lua 
[W 2016/01/14 10:18:49] [async.lua] GET echo.websocket.org:80/ => 101 Switching Protocols 59ms
instance of class HTTPParser
[S 2016/01/14 10:18:49] [websocket.lua] WebSocketClient connection open ws://echo.websocket.org
connected
Hello World!
[S 2016/01/14 10:18:49] [websocket.lua] WebSocketClient closed ws://echo.websocket.org 87ms
kernelsauce commented 8 years ago

Yes, sorry for the delay. I haven't been able to reproduce this problem. Please describe your version of Turbo and also platform you are running.

cinterloper commented 8 years ago

LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/ 4.3.3-2-ARCH #1 SMP PREEMPT x86_64 GNU/Linux Could one of you all link to the lua files you are using so i can double check i get the same results?

yurayko commented 8 years ago

Debian jessie x86_64, luajit 2.0.4, rebuilded from stretch.

git clone https://github.com/kernelsauce/turbo
cd turbo
sudo make install
$luajit t.lua 
[W 2016/01/26 15:23:09] [async.lua] GET echo.websocket.org:80/ => 101 Switching Protocols 346ms
instance of class HTTPParser
[S 2016/01/26 15:23:09] [websocket.lua] WebSocketClient connection open ws://echo.websocket.org
connected
Hello World!
[S 2016/01/26 15:23:09] [websocket.lua] WebSocketClient closed ws://echo.websocket.org 476ms
^Cluajit: /usr/local/share/lua/5.1/turbo/epoll_ffi.lua:91: interrupted!
stack traceback:
        /usr/local/share/lua/5.1/turbo/epoll_ffi.lua:91: in function 'poll'
        /usr/local/share/lua/5.1/turbo/ioloop.lua:449: in function '_event_poll'
        /usr/local/share/lua/5.1/turbo/ioloop.lua:442: in function 'start'
        t.lua:45: in function 'getws'
        t.lua:56: in main chunk
        [C]: at 0x00404420

t.zip

cinterloper commented 8 years ago
[grant@silverbullet ~]$ cd Downloads/
[grant@silverbullet Downloads]$ unzip t.zip 
Archive:  t.zip
  inflating: t.lua                   
[grant@silverbullet Downloads]$ nano -wc t.lua 
[grant@silverbullet Downloads]$ luajit t.lua 
[W 2016/01/28 00:01:38] [async.lua] GET echo.websocket.org:80/ => 400 Bad Request 200ms
[E 2016/01/28 00:01:38] Excpected 101, was 400, can not upgrade.
cinterloper commented 8 years ago

There is something wierd about my lua setup, i created a Fresh arch linux install, and it seems to work.