vendredi 6 mars 2015

Make two socket connections talk to each other

Here is the scenario...


I am working on an app I had an idea for, I'm building it in ember with an express backend. I am using the express-ws so I can run the ws websocket package inside express better. I was not able to get just ws to work with express.


My app will have two people connecting to two different url's that are socket connections, so that they can send and receive information to the server without the other getting it. At least that's the way I've come up in my mind to do it.


What I want is when one user does an interaction over the socket, for that socket to send a message to the other socket to perform an action and send it's information to the user connected on it.


I hope that makes sense. With express-ws here is what I have done so far which works at a basic level.



var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});

app.get('/', function(req, res, next){
console.log('browser connected');
res.send('welcome to the api browser');
});

app.ws('/', function(ws, req) {
console.log('socket connected');
var object = {
message: 'welcome to the socket api',
time: Date.now().toString()
}
ws.send(JSON.stringify(object));
});

app.listen(1337);


I haven't made the other connection yet but for the time being it will be the same, but when the user on one connection sends a certain message to their socket, I want that socket to perform something and then pass some data to the other socket so it can send some information to it's user.





Aucun commentaire:

Enregistrer un commentaire