I am trying to write a simple node.js server to share some simple data between two Node-Webkit apps. I am using Construct 2 which has a Websocket object enabling communication over Websockets but my knowledge is so lacking... I found node.js with its Websocket and socket.io setup but I have not been able to get this working.
All the examples and introductory information I can find starts with a chat room and gets more complicated from there. Even the chat room is beyond me because I am trying to filter out things that need human input and interfaces versus just the meat and bones of how to receive and send messages.
All I need is for one app to transmit a number and the other app to receive it and react to it. I don't need "chat" I don't need much other than just a simple pass a variable but I can't get it to work.
Can someone point me to a good place to start? All these tutorials about creating chat rooms assume I am serving up web pages and that's not at all my goal... I need a simpler tutorial!
App1 connects to server and sends a message of "1234" when the user hits a button. App2 connects and when it receives a message from the server it sets a variable to the text in the message and then responds to it.
It doesn't seem like this should be so hard but I am failing miserably trying to simplify what seems to be overly complex chat room applications into a super simple basic message relay system. Both apps are running on the LocalHost on the same machine.
I am programming, if you want to call it that, with Construct 2 because I don't know what I'm doing and I can stumble around in Construct 2 and get decent results...
Thanks!!
From What I can tell. You need can do this via two websocket servers
Creating a web socket server (http://ahoj.io/nodejs-and-websocket-simple-chat-tutorial) :
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
// process HTTP request. Since we're writing just WebSockets server
// we don't have to implement anything.
});
server.listen(1337, function() { });
// create the server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
// This is the most important callback for us, we'll handle
// all messages from users here.
connection.on('message', function(message) {
if (message.type === 'utf8') {
// process WebSocket message
}
});
connection.on('close', function(connection) {
// close user connection
});
});
Opening a web socket to talk to the server (http://einaros.github.io/ws/):
var WebSocket = require('ws')
, ws = new WebSocket('ws://localhost:/port');
ws.on('open', function() {
ws.send('something');
});
ws.on('message', function(message) {
console.log('received: %s', message);
});
App 1 -> Creates a websocket to send data to the Server
Server -> Creates a websocket server to listen to App1 and creates a websocket to relay messages to App 2
App 2 -> Creates a websocket server to listen to the Server relaying the message
I am not sure what your exact goals are. But, if they are to just send messages between App1 and App2. You can establish a websocket between the two of them directly and cut out the server