Hello everyone.

We have decided to phase out the Mixed Reality Forums over the next few months in favor of other ways to connect with us.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

The plan between now and the beginning of May is to clean up old, unanswered questions that are no longer relevant. The forums will remain open and usable.

On May 1st we will be locking the forums to new posts and replies. They will remain available for another three months for the purposes of searching them, and then they will be closed altogether on August 1st.

So, where does that leave our awesome community to ask questions? Well, there are a few places we want to engage with you. For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality. If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers. And always feel free to hit us up on Twitter @MxdRealityDev.

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.