PageRenderTime 271ms CodeModel.GetById 261ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/socket.js

https://github.com/englishtown/Lettershowdown
JavaScript | 107 lines | 65 code | 25 blank | 17 comment | 20 complexity | f058c3cbbecc5852f250a777c7bc92b2 MD5 | raw file
  1var socket;
  2var mySID;
  3
  4function socket_connect() 
  5{
  6	//local test
  7	socket = io.connect('http://localhost');
  8	
  9	//local ws test (working)
 10	//socket = io.connect( 'http://localhost', {port:14584, rememberTransport: false} );
 11	
 12	//nodester (working)
 13	//socket = io.connect( null, {port:80, rememberTransport: false} );
 14	
 15	//heroku (working)
 16	//socket = io.connect( null, {port:80} );
 17	
 18	//nodester ws test. (working)
 19	//socket = io.connect( 'http://lettershowdown.nodester.com/', {port:80, rememberTransport: false} );
 20	
 21	
 22	//handle socket message event
 23	socket.on( 'message', function ( data ) 
 24	{
 25		console.log( "socket message: " + data.sEvent );
 26		
 27		//triggered on client connection
 28		if ( data.sEvent == "connection" )
 29		{
 30			mySID = data.mySID;
 31			console.log( "Socket connected. My sID: " + mySID );
 32			$('#eventDispatcher').trigger( 'socket_connection', data );
 33		}
 34		
 35		//triggered on other user connection
 36		else if ( data.sEvent == "user_connect" )
 37		{
 38			console.log( "New user connected. User sID: " + data.sID );
 39			$('#eventDispatcher').trigger( 'socket_user_connect', data );
 40		}
 41		
 42		//triggered on other user spawn
 43		else if ( data.sEvent == "user_spawn" )
 44		{
 45			$('#eventDispatcher').trigger( 'socket_user_spawn', data );
 46		}
 47		
 48		//triggered on other user game viewport interaction
 49		else if ( data.sEvent == "user_avatar_interaction" )
 50		{
 51			$('#eventDispatcher').trigger( 'socket_user_avatar_interaction', data );
 52		}
 53		
 54		//triggered on user points update
 55		else if ( data.sEvent == "user_points_update" )
 56		{
 57			$('#eventDispatcher').trigger( 'socket_user_points_update', data );
 58		}
 59		
 60		//triggered on other user game viewport interaction
 61		else if ( data.sEvent == "user_server_action" )
 62		{
 63			$('#eventDispatcher').trigger( 'socket_user_server_action', data );
 64		}
 65		
 66		//triggered on other user disconnect
 67		else if ( data.sEvent == "user_disconnect" )
 68		{
 69			$('#eventDispatcher').trigger( 'socket_user_disconnect', data );
 70		}
 71	});
 72}
 73
 74function socket_disconnect() 
 75{
 76	socket.disconnect();
 77}
 78
 79function socket_client_spawn( data ) 
 80{
 81	data.sEvent = "client_spawn";
 82	socket.emit( 'message', data );
 83}
 84
 85function socket_client_avatar_interaction( data ) 
 86{
 87	data.sEvent = "client_avatar_interaction";
 88	socket.emit( 'message', data );
 89}
 90
 91function socket_client_server_action( data ) 
 92{
 93	data.sEvent = "client_server_action";
 94	socket.emit( 'message', data );
 95}
 96
 97function socket_interaction( sEvent, data )
 98{
 99	data.sEvent = sEvent;
100	socket.emit( 'message', data );
101}
102
103
104
105
106
107