Hi,
Don't know if I understood correctly what you are trying to accomplish. What I understood is you have a server in node.js that is serving the Cesium page, you want to connect a websocket to this server in order to exchange data between the server and different clients that connect to them?
A library that I use for websockets in node.js is Socket.io ( http://socket.io/ ), another module I usually use is Express.js (for the webserver part). You can create a socket.io instance and listen to the same port as the normal webserver:
var app = require('express')();
var http = require('http').Server(app); // Normal HTTP server
var io = require('socket.io')(http); // Attach Socket.io to the HTTP server in order to listen to the same port
app.get('/', function(req, res){
res.send('<h1>Hello world</h1>');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(80, function(){
console.log('listening on *:80');
});
On the client you could have something like:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io(); // here you can set a custom IP / Port. The default is to connect to the server that serves the page
</script>
With this you have all Clients connected to the socket.io server, and you can exchange information by triggering events either on the server or on the client.
So lets imagine you want to listen for events containing CZML objects from the server. On the client you would add a listener:
socket.on('czmlObject', function(msg){
// Msg comes in JSON format and contains the data sent by the server
});
The server can then trigger this event when needed. You can send to a specific socket (by keeping track of the connections to the server) or send to every connected socket. So lets imagine a client sends data to the server and the server sends to all clients connected except the sender:
// Client
socket.emit('data', {...}); // send some JSON object to the server
// Server
io.on('connection', function(socket){
console.log('a user connected');
socket.on('data', function(msg) {
socket.broadcast.emit('czmlObject', msg);
});
});
Hope this helps you in some way. If what understood of your issue is wrong, then try to clarify and maybe I can help you.
Terça-feira, 16 de Setembro de 2014 15:51:26 UTC+1, Chris M. escreveu: