Skip to content

Instantly share code, notes, and snippets.

@llekn
Created March 17, 2017 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llekn/2affb8b165afbeebce7433853a0f2a37 to your computer and use it in GitHub Desktop.
Save llekn/2affb8b165afbeebce7433853a0f2a37 to your computer and use it in GitHub Desktop.
Websocket server using Eventmachine
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