Last active
July 10, 2020 02:56
-
-
Save nathanclark/54dc317601f941cc90aac1848ad3983f to your computer and use it in GitHub Desktop.
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
<% if current_user %> | |
<script> | |
var pusher = new Pusher('HIDDEN', { authEndpoint: '/pusher/auth' , cluster: 'HIDDEN', | |
auth: { | |
headers: { | |
'X-CSRF-Token': "<%= form_authenticity_token %>" | |
} | |
} | |
}); | |
var channel = pusher.subscribe('presence-HIDDEN-<%= Rails.env %>') | |
channel.bind('pusher:subscription_succeeded', function() { | |
var me = channel.members.me; | |
var userId = me.id.substring(0,me.id.indexOf('-')); | |
var userInfo = me.info; | |
var sessionCount = 0 | |
for(var key in channel.members.members) { | |
var value = channel.members.members[key] | |
var memberId = key.substring(0,key.indexOf('-')) | |
if(userId == memberId){ | |
sessionCount = sessionCount+1 | |
} | |
if(sessionCount >25) | |
{ | |
setTimeout( | |
function() | |
{ | |
$.ajax({ | |
url: '/my/users/sign_out', | |
type: 'DELETE', | |
success: function(result) { | |
location.reload(); | |
} | |
}); | |
}, 5000); | |
} | |
} | |
}); | |
channel.bind('pusher:subscription_error', function(status) { | |
console.log(status) | |
}); | |
</script> | |
<% end %> |
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
def auth | |
require 'pusher' | |
pusher = Pusher::Client.new( | |
app_id: 'HIDDEN', | |
key: 'HIDDEN', | |
secret: 'HIDDEN', | |
cluster: 'us2', | |
use_tls: true | |
) | |
if current_user | |
response = pusher.authenticate(params[:channel_name], params[:socket_id], { | |
user_id: current_user.id.to_s + '-' + DateTime.now.strftime('%s'), # => required | |
user_info: { # => optional - for example | |
name: current_user.name, | |
email: current_user.email + '-' + DateTime.now.strftime('%s') | |
} | |
}) | |
render json: response | |
else | |
render text: 'Forbidden', status: '403' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment