Created
March 17, 2017 14:01
-
-
Save llekn/2affb8b165afbeebce7433853a0f2a37 to your computer and use it in GitHub Desktop.
Websocket server using Eventmachine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'em-websocket' | |
EM.run do | |
EM::WebSocket.run(host: '0.0.0.0', port: 8080) do |ws| | |
ws.onopen do |handshake| | |
puts 'WebSocket connection open' | |
# Access properties on the EM::WebSocket::Handshake object, e.g. | |
# path, query_string, origin, headers | |
# Publish message to the client | |
ws.send "Hello Client, you connected to #{handshake.path}" | |
end | |
ws.onclose { puts 'Connection closed' } | |
ws.onmessage do |msg| | |
puts "Recieved message: #{msg}" | |
ws.send "Pong: #{msg}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment