简体   繁体   中英

Send Post Data or Get Data on socket.io

I want to send a POST or GET data in socket.io but I can't receive any data. I want to send the data outside the connection. Below is my code:

Server code: app.js

io.sockets.on('connection', function (socket) {
    console.log('You Have successfully Connected to the server');
});

io.sockets.on('checkUser', function (data) {
    io.sockets.emit('callback', {
        data: data
    });
});

Client Side: app.js

$('.key').click(function () {
    var splitItem = $(this).find('.inputKey').val().split('-');;
    var info = {
        userid: user_id,
        itemid: splitItem[0], // keyid
        box: splitItem[1], // boxid
        username: username,
    }
    socket.emit('checkUser', {
        data: info 
    });
});

socket.on('callback', function (data) {
    console.log(data);
});

if you just want to send a simple http call think again if you have to use socket.io, socket.io built mainly for sending data between client and server in realtime.

to use node.js for querying your mysql database and much more check out http://expressjs.com/ web application framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM