Socket.io error on facebook instant games build

Socket.io error on facebook instant games build
0
#1

Hey everyone… hopefully you can help me :slight_smile:

I have created a web server using node.js.

// server.js

    var fs = require( 'fs' );
    var express = require('express');
    var app = express();
    var servIO = require('https').createServer({ 
    key: fs.readFileSync('privkey.pem'),
    cert: fs.readFileSync('fullchain.pem') 
    },app);
  
    app.use('/client',express.static(__dirname + '/client'));
     
   
    servIO.listen(8002);
    console.log("Server started.");

    
    io.on('connected', function(socket) {
	console.log('a user connected');
	socket.emit('connected', 'hello');
    });

// helloworld.js

cc.Class({
extends: cc.Component,

properties: {
    label: {
        default: null,
        type: cc.Label
    },
    text: 'Hello, World!'
},

// use this for initialization
onLoad: function () {
    let self = this;
        if (cc.sys.isNative) {
        window.io = SocketIO.connect;
    } else {
        require('socket.io');
    }
    
    var socket = io('https://www.mydomainname.org:8002');
	
    socket.on('connected', function (msg) {
        self.label.string = msg;
    });
},

// called every frame
update: function (dt) {

},
});

If i do not build the app;
when i run this from the cocos creator simulator it connects to the server and the server logs that there is a connection. It appears to work however the label is not updated leading me to believe the emit function is not being received.

if i run this from a web browser it does not work;
The label is not updated and in my server console it repeatedly fires the connect function.

if i build this as a facebook instant game;
when testing using the standard facebook test method from this page https://www.facebook.com/embed/instantgames/gameID/player?game_url=https://localhost:8080 (which i have successfully been using with non socket io games) then i get this error;

Uncaught ReferenceError: io is not defined
at CCClass.onLoad (project.js:1)
at CCClass.eval [as _invoke] (eval at o (cocos2d-js-min.js:1), :3:65)
at CCClass.invoke (cocos2d-js-min.js:1)
at CCClass.activateNode (cocos2d-js-min.js:1)
at CCClass._activate (cocos2d-js-min.js:1)
at e.runSceneImmediate (cocos2d-js-min.js:1)
at cocos2d-js-min.js:1
at r. (cocos2d-js-min.js:1)
at cocos2d-js-min.js:1
at cocos2d-js-min.js:1

I have followed this https://github.com/moonlightpoet/SocketIOTest

just to clarify, I have socket.io working with my server from a web page. I just cant figure out how to implement it through a game i’m making with cocos creator.

Can anyone tell me what the problem is please? I cant get this to work at all :frowning:
Thanks in advance :slight_smile:

#2

There is an example in the example-cases project

#3

did you solve your problem with fb instant.

#4

pretty sure the event is connect not connected on the cocos side.

Also, isn’t it supposed to be var io = require('socket.io');
I don’t think you are declaring the variable.