Socket.IO NodeJS not working in HoloLens

PeriPeri
edited September 2018 in Questions And Answers

I have implementing sockets in hololens app. App is working fine when run in Unity editor. But not working on hololens. Sample code below:

 public SocketManager Manager;

        void ConnectSocket()
        {
             SocketOptions options = new SocketOptions();
                    options.AutoConnect = false;
                    Manager = new SocketManager(new Uri("http://holo.us.openode.io/socket.io/"), options);
                    // The argument will be an Error object.
                    Manager.Socket.On(SocketIOEventTypes.Error, (socket, packet, args) => Debug.LogError(string.Format("Error: {0}", args[0].ToString())));
                    Manager.GetSocket("/");

                    Manager.Socket.On("joinroom", OnJoinedRoom);
                    Manager.Socket.On("sendchat", OnFirstMessageRecieved);
                    Manager.Open();
                    Manager.Socket.Emit("adduser", 11, "User001");
         }           
          private void OnJoinedRoom(Socket socket, Packet packet, object[] args)
          {
            Debug.Log("Received message from server");
          }
          private void OnFirstMessageRecieved(Socket socket, Packet packet, object[] args)
          {
             Debug.Log("Receive...");
           }

NodeJS Server Code:

    io.sockets.on('connection', function (socket) {

        // when the client emits 'adduser', this listens and executes
        socket.on('adduser', function (username,roomid) {
            // store the username in the socket session for this client
            socket.username = username;
            // store the room name in the socket session for this client
            socket.room = roomid;

            // add the client's username to the global list
            usernames[username] = username;
            // send client to room 1
            socket.join(roomid);
            // echo to client they've connected
            socket.emit('joinroom', 'SERVER', 'you have connected to ' + roomid,username);
            console.log('User : '+username+'connected to room : ' + roomid);
            // echo to room 1 that a person has connected to their room
            socket.broadcast.to(roomid).emit('newuserjoined', 'SERVER', username + ' has connected to this room ' + roomid);
            socket.emit('updaterooms', rooms, roomid);
        });

        // when the client emits 'sendchat', this listens and executes
        socket.on('sendchat', function (data) {
            // we tell the client to execute 'updatechat' with 2 parameters
            io.sockets.in(socket.room).emit('updatechat', socket.username, data);
        });

    server.listen(app.get('port'),function(){
    console.log("Server is running...");
    });

Note: Enabled InternetClientServer And PrivateNetworkClientServer.
Unity version: Unity 2017.2.2p1

Used BestHttp unity asset to implement socket.io(https://assetstore.unity.com/packages/tools/network/best-http-10872).

Comments

Sign In or Register to comment.