Created
October 31, 2013 05:50
-
-
Save crertel/7244841 to your computer and use it in GitHub Desktop.
Simple example code for geo-based chatting.
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
$(function(){ | |
ws = new WebSocket("ws://localhost:8080"); | |
ws.onmessage = function(evt) { | |
if ($('#chat tbody tr:first').length > 0){ | |
$('#chat tbody tr:first').before('<tr><td>' + evt.data + '</td></tr>'); | |
} else { | |
$('#chat tbody').append('<tr><td>' + evt.data + '</td></tr>'); | |
} | |
}; | |
ws.onclose = function() { | |
}; | |
ws.onopen = function() { | |
navigator.geolocation.getCurrentPosition( function (pos) { | |
var msg = { text: "Joins the chat!", | |
locus: [pos.coords.latitude, pos.coords.longitude] }; | |
ws.send( JSON.stringify(msg)); | |
}); | |
}; | |
$("form").submit(function(e) { | |
if($("#msg").val().length > 0) { | |
var theMessage = $("#msg").val(); | |
navigator.geolocation.getCurrentPosition( function (pos) { | |
var msg = { text: theMessage, | |
locus: [pos.coords.latitude, pos.coords.longitude] }; | |
ws.send( JSON.stringify(msg)); | |
}); | |
$("#msg").val(""); | |
} | |
return false; | |
}); | |
$("#clear").click( function() { | |
$('#chat tbody tr').remove(); | |
}); | |
}); |
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
#thanks https://github.com/amscotti/em_sinatra_chat | |
require 'rubygems' | |
require 'em-websocket' | |
require 'yajl' | |
require 'haml' | |
require 'sinatra/base' | |
require 'thin' | |
require 'json' | |
$channel = EM::Channel.new | |
#thanks http://codingandweb.blogspot.com/2012/04/calculating-distance-between-two-points.html | |
# distance is in miles | |
def distance_between_lat_long_pairs( a, b ) | |
dtor = Math::PI/180 | |
r = 3959 | |
rlat1 = a[0] * dtor | |
rlong1 = a[1] * dtor | |
rlat2 = b[0] * dtor | |
rlong2 = b[1] * dtor | |
dlon = rlong1 - rlong2 | |
dlat = rlat1 - rlat2 | |
a = (Math::sin(dlat/2)**2) + Math::cos(rlat1) * Math::cos(rlat2) * (Math::sin(dlon/2)**2) | |
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a)) | |
d = r * c | |
return d | |
end | |
# hash mapping websockets to latlong pairs. | |
openConnections = {} | |
EventMachine.run do | |
class App < Sinatra::Base | |
get '/' do | |
haml :index | |
end | |
end | |
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws| | |
ws.onopen { | |
openConnections.each do | k,locus| | |
k.send("Hello #{ws.object_id}!") | |
end | |
ws.onmessage { |msg| | |
message = JSON.parse(msg) | |
# add ourselves to the hash | |
openConnections[ws] = message["locus"] | |
origin = message["locus"] | |
# message everyone else within a mile radius | |
openConnections.each do | k, dest| | |
distance = distance_between_lat_long_pairs( origin, dest) | |
k.send("#{ws.object_id}: #{message["text"]}") unless distance > 1 | |
end | |
} | |
ws.onclose { | |
openConnections.each do | k,locus| | |
k.send("Goodbye #{ws.object_id}!") | |
end | |
} | |
} | |
end | |
App.run!({:port => 3000}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment