A
A
Alex2014-08-21 15:17:33
Node.js
Alex, 2014-08-21 15:17:33

How to work with sockjs?

Good afternoon!
I want to understand the work of node.js + sockjs.
I start the server:

var http = require('http');
var $server = 'test.lan';
if (http) {
    var sockjs = require('sockjs');

    var echo = sockjs.createServer();

    echo.on('connection', function(conn) {
        conn.on('data', function(data_id) {
            conn.write(data_id);
        });
        conn.on('close', function() {});
    });

    var server = http.createServer();
    echo.installHandlers(server, {prefix:'/echo'});
    server.listen(5555, $server);
}

on the html page client:
var sock = new SockJS('http://test.lan:5555/echo');
    sock.onopen = function() {
        console.log('open');
    };
    sock.onmessage = function(e) {
        console.log('result data: ', e.data);
    };
    sock.onclose = function() {
        console.log('close');
    };
    // отправляем данные
    setInterval(function() { sock.send(40); }, 1000);

Interested in the following.
Here we sent the server the value 40, let's say this is the id of some record in the database. sock.send(40);
How can I check the data on the server side and return it to the client?
To put some kind of plugin for node.js on mysql or how can I turn to php?
Here we get id
conn.on('data', function(data_id) {
          // что делать дальше?
});

Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question