how to receive custom events?

Options
mindlube
edited November 2012 in JavaScript and TypeScript
In the C# client, I just implement IPhotonPeerListener.OnEvent( EventData eventData ) and my custom events work fine. Not the case in the JS api...

Looking at the JS API docs, all the methods for adding event handlers take a string "name" as the 1st parameter. What if it's a custom event code (byte) ?

Not using Lite, by the way.

Comments

  • Reading through the client source code, this should work. But it's like the custom events are just not being received at all. After connecting it just times out and disconnects. The server is definitely sending custom events.
    <script src="PhotonPeer.Lite.debug.js"></script>
    <!--
       also tried srcing these, just copied from the API docs
        <script src="PhotonPeer.IO.js"></script>
        <script src="PhotonPeer.js"></script>
    -->
    
        <script type="text/javascript">
          
    function init() {
        
        var photon = {
    	peer: new PhotonPeer(),  
    	host: 'localhost', 
    	port: 9090
        };
       
        photon.peer.eventCodes.metadata = '250';
    
        console.log(photon.peer.eventCodes);
    
        photon.peer.addEventListener('connecting', function() {
    	console.log('connecting...');
        });
    
        photon.peer.addEventListener('connect', function() {
    	console.log('connected!');
        });
        
        photon.peer.addEventListener('disconnect', function() {
    	console.log('disconnected.');
        });
        
        photon.peer.addEventListener('timeout', function() {
    	console.log('timeout.');
        });
        
    // try to register a listener for  the event code 250 (aka metadata)
    
        photon.peer.addEventListener(photon.peer.eventCodes.metadata, 
    					function(event) {
    					    console.log('event recd');
    					});
    
        photon.peer.addCustomEventListener(photon.peer.eventCodes.metadata, 
    					function(event) {
    					    console.log('custom event recd');
    					});
    
      photon.peer.addEventListener('250', 
    					function(event) {
    					    console.log('custom event recd');
    					});
    
        photon.peer.addCustomEventListener('250', 
    					function(event) {
    					    console.log('custom event recd');
    					});
    
        photon.peer.addCustomEventListener('cus_250', 
    				       function(event) {
    					    console.log('custom event recd');
    					});
    
        photon.peer.addEventListener('cus_250', 
    					function(event) {
    					    console.log('custom event recd');
    					});
        photon.peer.addCustomEventListener('cus_metadata', 
    					function(event) {
    					    console.log('custom event recd');
    					});
    
        photon.peer.addEventListener('cus_metadata', 
    					function(event) {
    					    console.log('custom event recd');
    					});
        photon.peer.addEventListener('metadata', 
    					function(event) {
    					    console.log('custom event recd');
    					});
        
        photon.peer.connect( photon.host, { port: photon.port, isDebugging: true });
    
    }
    
    </script>
    
  • And the relevant part of my applicationBase implementation
    public enum EventCode : byte
    		{
    			ServerMetadata = 250
    		}
    		
    		protected override void Setup()
            {			
    			SetupLogging();
    
    			Protocol.AllowRawCustomValues = true;
    			
    			executionFiber.Start();
    			executionFiber.ScheduleOnInterval( () => { 
    				if(peers == null || peers.Count == 0)
    					return;
    				log.DebugFormat("sending event code {1} to {0} peers", peers.Count,  (byte)EventCode.ServerMetadata);
    				Dictionary<byte,object> opParams =  new Dictionary<byte, object>();
    				opParams[0] = Metadata();
    				EventData ed = new EventData( (byte) EventCode.ServerMetadata, opParams);	
    				App.Instance.BroadCastEvent(ed, peers, new SendParameters());
    			},
    			0, 1000);	
    
    			log.InfoFormat("Created application instance: type={0}", Instance.GetType());			
            }
    
  • [Deleted User]
    Options
    Your application needs to inherit from Lite for the initial connect which is required to happen compatible with Socket.IO protocol. A plain socket connection is not sufficient.

    Without you just have a socket connection but no application being listened to, hence no events registered in your client.

    Custom events' codes are received as strings since they are encoded in JSON keys. So listening for e.g. '42' will work when the above connection with your app is established.
  • OK I was able to communicate custom ops and events with my server app like this after using PhotonPeer.Lite() in the client.
    photon.peer.customOperation('12');
    ...
    photon.peer.addCustomEventListener('4', 
    					function(event) {
    					    console.log('event recd...');
    					});
    
    OK it works - cool!

    However, two bits of feedback about the JS api

    1) there is no way to specify the name of the application/game in the client API (that I can find).For example the method in the C# API:
    PhotonPeer.Connect ( host, "myApp" );

    2) Even though I'm not using Lite on the server side, the JS api forces me create a Lite Peer. Which is kinda confusing. Stefan's explanation about "plain socket connection isn't sufficient" didn't really make sense to me.
  • Tobias
    Options
    Thanks for the feedback. Stefan will take a look when he's back from vacation and catched up with everything.
  • Sounds good- thanks. Low priority; I'm not actually using the JS api for my game. Just playing around with it for future ideas, or maybe a dashboard type of usage.
  • [Deleted User]
    Options
    1) There is no way to specify the game/application to connect to in the JS API.

    2) The JS API as is is specific to Lite. A plain socket / Photon connection is not sufficient, you have to have a Lite application listening on your websocket port to start with ... which answers (1) as well ... :)

    fyi
    Stefan
  • OK but why is the JS API tied to the Lite application? None of the other client APIs are limited in that way, are they? - other than Cloud / PUN , right?
    Just trying to understand. Thanks
  • Tobias
    Options
    JS clients support only a subset of usually used data Types and they use their own protocol (websockets), which is special as well.
    In combination this requires a special treatment on the server side, which is only implemented in Lite, currently.

    So, to be more specific: The JS library is not tied 1:1 to Lite but currently only Lite understands JS clients out of the box.

    As with other libraries, you could modify Lite, add events and operations and even connect to other applications. The latter requires some extra work on the server side, which we didn't do yet for you.
  • OK, just want to say it's puzzling because as I posted above, in my testing I can communicate custom ops and events with my (non-Lite) server app but only after using PhotonPeer.Lite() in the client.
  • [Deleted User]
    Options
    Back when the lib has been developed it was that tightly coupled and therefore might be puzzling now, sorry.

    However, I am now curious where the behavior you outline results from but I guess this is hardly possible w/o taking a look into the actual client and server code to generate some tests from it ...

    If you like to Tobias and I can have a closer look when you send it over.
  • Events and responses inside js api are just for logical separation between responses sent on your request and events. On low level they both are almost the same. Server will send to your JS client just some data, where one of the field will tell you if thats event or response. You can take a look inside PhotonPeer._onMessageReceived method.
    mindlube wrote:
    1) there is no way to specify the name of the application/game in the client API (that I can find).For example the method in the C# API:
    PhotonPeer.Connect ( host, "myApp" );

    2) Even though I'm not using Lite on the server side, the JS api forces me create a Lite Peer. Which is kinda confusing. Stefan's explanation about "plain socket connection isn't sufficient" didn't really make sense to me.

    1) On server side you can't have multiple apps listening on one websocket. You can have only one app for one WS port. Take a look inside config, when all server apps are being registered. For lite apps there is default websocket on port 9090, for your custom app you can have for example port 9091.


    2) While js api is still beta and tied to Lite, you can use this unnoficial custom version for PhotonPeer ,no the Lite().

    https://bitbucket.org/narushevich/photonpeer

    Everything except PhotonPeer is unmodified. I use it as a base for extending.