NPM package for SDK?

I have been looking to see if there is a NPM package (for use with things like nodejs. I have not found one.

Am I missing it?

If not, there REALLY aught to be an npm package...

Comments

  • Apart from that, I am having a hell of a time getting this to import into my main.ts file in nodejs...
    const photon = require('libraries/photonsdk/Photon-Javascript_SDK');
    // import { Exitgames } from 'libraries/photonsdk/Photon-Javascript_SDK';

    It says module not found for the import, but doing a standard require does not seem to work even a little.
  • I have started to make my own modifications to try and get it to work, but after adding export too all of the photon modules, I still get:

    Unless I am doing something very wrong, this seems to be not ready for a node environment.
  • vadimvadim ✭✭✭
    edited January 25
    nodejs is not officially supported.

    Below is a sample app running in nodejs.
    To load Photon lib from local storage, 'fs' and 'vm' package are used.
    Since Photon js sdk relies on browser's WebSocket object, you need to implement its interface with websocket you use in nodejs.
    
    const appID = "app-id"
    const util = require('util')
    const assert = require('assert')
    const vm = require("vm");
    const fs = require("fs");
    const load = function(path, context) {
      var data = fs.readFileSync(path);
      vm.runInThisContext(data, path);
    }
    
    load("Photon-Javascript_SDK.js");
    
    var wsClient = require("ws")
    WebSocket = function (uri) {
    	var self = this;
    	this.socket = new wsClient(uri, {protocol: "Json"});
    	this.socket.onopen = function () {
    		self.onopen();
    	};
    	
    	this.socket.onerror = function (error) {
    		self.onerror(error);
    	};
    	
    	this.socket.onclose = function (ev) {
    		self.onclose(ev);
    	};
    
    	this.socket.onmessage = function (message) {
    		if (message.type === 'message') {
    			self.onmessage({data:message.data});
    		}
    		return true;
    	};
    }
    
    WebSocket.prototype.send = function (data) {
    	if(this.socket)
    		this.socket.send(data);
    }
    
    WebSocket.prototype.close = function () {
    	if(this.socket)
    		this.socket.close();
    }
    
    var LBC = Photon.LoadBalancing.LoadBalancingClient;
    var lbc = new LBC(Photon.ConnectionProtocol.Ws, appID, "1.0");
    
    function raiseEvent() {
    	console.log("Event Out");
    	lbc.raiseEvent(1, { message: "hello" }, { receivers: Photon.LoadBalancing.Constants.ReceiverGroup.All});
    }
    
    lbc.onStateChange = function (state) {
    	// "namespace" import for static members shorter access	
    	console.info("State:", LBC.StateToName(state));
    	
    	switch (state) {
    		case LBC.State.JoinedLobby:
    			this.joinRoom("node-", {createIfNotExists: true});
    			break;
    		case LBC.State.Joined:
    //			lbc.disconnect();
    			raiseEvent();
    			break;
    		case LBC.State.Disconnected:
    			lbc.connectToRegionMaster("EU");
    			break;
    		default:
    			break;
    	}
    };
    
    lbc.onEvent = function (code, data) {
    	console.info("Event In:", code, data);
    }
    
    lbc.onOperationResponse = function (errorCode, errorMsg, code, content) {
    //	console.info("op resp:", errorCode, errorMsg, code, content);
    	if (errorCode) {
    		switch (code) {
    //			case Photon.LoadBalancing.Constants.OperationCode.JoinRandomGame:
    	//			break;
    			default:
    				console.error("Operation Response error:", errorCode, errorMsg, code, content);
    				break;
    		}
    	}
    };
    
    lbc.connectToRegionMaster("EU");
    
    
  • edited January 25
    I see. I could be wrong, but won't electron complain if I use websockets in the main thread?

    Really they aught to just make an NPM package... I looked into it, but it looks like it would require a bit of restructuring.
  • edited January 25
    Thanks for the info. Running through it now to get it working.
  • So not sure how this is being strung together...
    When I try to run, I still get
    ReferenceError: WebSocket is not defined
        at NameServerPeer.PhotonPeer.connect (lib/photonsdk/Photon-Javascript_SDK.js:442:36)
        at LoadBalancingClient.connectToNameServer (lib/photonsdk/Photon-Javascript_SDK.js:1778:41)
        at LoadBalancingClient.connectToRegionMaster (lib/photonsdk/Photon-Javascript_SDK.js:2252:31)
        at PhotonManager.start (C:\Repositories\ArcMages\arcmages-client\src\main\photonManager.js:102:13)
        at Object. (C:\Repositories\ArcMages\arcmages-client\main.js:19:8)
        at Object. (C:\Repositories\ArcMages\arcmages-client\main.js:121:3)
        at Module._compile (internal/modules/cjs/loader.js:693:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:704:10)
        at Module.load (internal/modules/cjs/loader.js:602:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:541:12)
    even though I am using your example.
  • vadimvadim ✭✭✭
    edited January 28
    WebSocket is defined as a global var in line #14 of code snippet i posted above.
    Does this code run alone work for you? Don't forget to set appID.
  • Played with it more, turns out that I needed to do quite a bit of rewriting of the SDK for it to work right - nodejs does not have a concept of a DOM, so Websocket was failing to run properly.
Sign In or Register to comment.