I'm trying to make an internet phone switch. Basically when someone presses a specific button on their phone it makes a GET call to my website and that GET call fires an event via socket.IO which everyone connected to the website can see.
I get the GET call just fine and my server notices it just fine. But because the phone can't render a webpage, I can't get it to fire off an event. If I just two different computers and two browsers everything works exactly how I want.
When I render a page I have on my "layout.jade" this code which fires off an event if you are in the right page.
var socket = io.connect('http://MYWEBPAGE.org:3000');
socket.on('connect', function () {
if ($('title')[0].text == "IMC_log") {
socket.emit('server_call');
}
});
socket.on('call', function () {
console.log("CALL HERE");
});
And on the server.js I have
io.sockets.on('connection', function (socket) {
socket.on('server_call', function () {
socket.broadcast.emit('call');
});
});
Using just browsers this works. But now when I use my phone, it won't fire off the "server_call" event, due to (probably) it can't render the page so it just never actually uses the "layout.jade". But as it does the GET call just fine, I would like to be able to fire off that event from my server.js file (or index.js, both are fine), alas I have no idea how to do that. If I try to fire it off like this:
app.get ("/log_get", function () {
socket.broadcast.emit('call');
});
It just whines that socket is not defined. The problem seems to be that it needs to have the io.sockets.on('connection', function (socket) {... in order to be able to recognize the "socket", but I can't do that because I already have one "on connection" there.
Help is appreciated!
Npm module socket.io-client did the job.