websocket example?

308 views
Skip to first unread message

Forrest Chang

unread,
Jul 27, 2015, 2:00:52 PM7/27/15
to Crystal
Is there a websocket example?  I can't figure out from the code and spec how to run one.

My guess it's something like:

require "http/server"

handler = HTTP::WebSocketHandler.new do |session|
   # what to do exactly, I'm not sure
end

HTTP::Server.new(8081, handler).listen

I can see that WebSocketHandler does some upgrade handshake, but not certain how to get it to work in a server

Thanks

Forrest

Keith Salisbury

unread,
Jul 28, 2015, 5:48:31 PM7/28/15
to Crystal, fkcha...@gmail.com
Me too, looking for the same answer...

Keith Salisbury

unread,
Jul 28, 2015, 7:16:45 PM7/28/15
to Crystal, fkcha...@gmail.com
Hi Forrest,

I found this which might help a little:

headers = HTTP::Headers {
"Upgrade": "websocket",
"Connection": "Upgrade",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ=="
}
request = HTTP::Request.new("GET", "/", headers)
response = handler.call request

https://github.com/manastech/crystal/blob/119ccc89065165bc937b3417e5ac2af2bab6e056/spec/std/http/server/handlers/websocket_handler_spec.cr

hth


On Monday, 27 July 2015 19:00:52 UTC+1, Forrest Chang wrote:

Forrest Chang

unread,
Jul 30, 2015, 12:37:20 PM7/30/15
to Crystal, keith.s...@alliants.com
I got the answer the other day w/help from adam12 on irc.  It turns out that HTTP::Request#keep_alive? is broken https://github.com/manastech/crystal/pull/892 will fix it.

That being said, you can monkey patch it yourself.  Here's a working echo server.  Note: it gets an error if the client does a websocket.close(), though the error catch apparently closes the socket which is sort of the right behavior, though entirely the wrong semantics.  I submitted this issue https://github.com/manastech/crystal/issues/1054 and if I can fix it, I'll submit a PR

Here's the code

require "http/server"
class HTTP::Request
  def keep_alive?
    case @headers["Connection"]?.try &.downcase
    when "keep-alive"
      return true
    when "close"
      return false
    when "upgrade"
      return false
    end

    case @version
    when "HTTP/1.0"
      false
    else
      true
    end
  end
end
handler = HTTP::WebSocketHandler.new do |session|
  session.onmessage do |message|
    puts "Received #{message}"
    pp session.@ws.send message

  end
end
HTTP::Server.new(8081, [handler, HTTP::LogHandler.new]).listen

Forrest Chang

unread,
Jul 30, 2015, 1:12:41 PM7/30/15
to Crystal, keith.s...@alliants.com, fkcha...@gmail.com
Looks like they just merged that pull request, if you're running from head, you won't need to monkey patch

Ary Borenszweig

unread,
Jul 30, 2015, 1:15:45 PM7/30/15
to crysta...@googlegroups.com, keith.s...@alliants.com, fkcha...@gmail.com
Yes, but please know that websocket is only partially implemented, so I don't think you'll be able to do a useful app with it right now.

--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at http://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/96dd9146-1913-4b3f-9e84-71750b788e3a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Ary Borenszweig         Manas Technology Solutions
[ar.phone]                      5258.5240       #ARY(279)
[us.phone]                      312.612.1050    #ARY(279)
[email]                         aboren...@manas.com.ar
[web]                           www.manas.com.ar
Reply all
Reply to author
Forward
0 new messages