microsoft / HoloJS

Provides a framework for creating holographic apps using JavaScript and WebGL.
MIT License
1.19k stars 114 forks source link

Connect ThreeJS to a server via socket.io #48

Closed bryanbocao closed 7 years ago

bryanbocao commented 7 years ago

My attempt was to send a message from a server to ThreeJSApp.

We wrote a server script and there's a node.js server running, whose IP address was 10.100.200.104 and was able to send messages through 'message'. We tested that any web page connected to the same network with the code below was able to get the message.

var serverIP = 'http://10.100.200.104'; // This should be the IP address that's assigned by your router
var messagePort = '8000';
var socket = io.connect(serverIP + ':' + messagePort);
socket.on('message', function (data) {
    console.log(data);
});

and sure the code should have imported the javascript file from https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js

In the "ThreeJSApp" sample, I simply put the code into app.js from https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js

and wrote the previous code into app.js:

but there were errors when running the

var socket = io.connect(serverIP + ':' + messagePort);

The error messages were error1 error2

Any tips?

Almost-Done commented 7 years ago

Is socket.io using WebSockets? Is yes, then there's some work to be done to implement support for them in the native layer. If not, then this could be an easy fix in the XHR implementation.

bryanbocao commented 7 years ago

Partially.

"Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here." from https://github.com/socketio/socket.io/

Socket IO protocol: https://github.com/socketio/socket.io-protocol

bryanbocao commented 7 years ago

In app.js, when I run

var serverIP = 'http://127.0.0.1'; // This should be the IP address that's assigned by your router
var messagePort = '8000';
console.log(serverIP);
var socket = io(serverIP + ':' + messagePort);

I got the message from Output window in Visual Studio 2015:

log: http://10.100.200.104
Exception thrown at 0x77062502 in threejsapp.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x01EC9CA4.
'threejsapp.exe' (Win32): Loaded 'C:\Windows\System32\urlmon.dll'. Symbols loaded.
'threejsapp.exe' (Win32): Loaded 'C:\Windows\System32\mlang.dll'. Symbols loaded.
Exception thrown at 0x77062502 in threejsapp.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x01ECCCC4.
Exception thrown at 0x77062502 in threejsapp.exe: Microsoft C++ exception: Js::JavascriptExceptionObject at memory location 0x01EC99BC.
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\objectevents.h, line 52
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\windowelement.cpp, line 149
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\windowelement.cpp, line 149
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\windowelement.cpp, line 149
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\windowelement.cpp, line 149
Failure in file c:\users\bryanbo.cao\documents\repos\ar-collaboration\src\holojs\holojs\holojshost\windowelement.cpp, line 149

But on the server which is running on another laptop, I could see that a new client was connected.

Almost-Done commented 7 years ago

There were a couple missing functions that socket.io needs (get/set headers, clearTimeout).

I implemented those in the develop/fix-socket-io and I got the chat sample working with this code:

    var serverIP = 'http://127.0.0.1';
    var messagePort = '3000';
    var socket = io.connect(serverIP + ':' + messagePort);

    socket.on('chat message', function (msg) {
        console.log(data);
    });

Can you please give it a try and let me know if it works for you?