简体   繁体   中英

How to connect Node.js and PHP server using SocketIO

How to send data from node server to PHP server(Apache) using Socket.io

Server Side:

var socket = require( 'socket.io' );
var http = require( 'http' );

var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  console.log('user connected!');

Client Side (PHP SERVER, Javascript code):

var socket = io.connect( 'http://localhost:8080' );

$( "#messageForm" ).submit( function() {
    var nameVal = $( "#nameInput" ).val();
    var msg = $( "#messageInput" ).val();
    socket.emit( 'message', { name: nameVal, message: msg } );
    });

    return false;
});

socket.on( 'message', function( data ) {
    var actualContent = $( "#messages" ).html();
    var newMsgContent = '<li> <strong>' + data.name + '</strong> : ' + data.message + '</li>';
    var content = newMsgContent + actualContent;

    $( "#messages" ).html( content );
});

  socket.on('foo', function (data) {
    console.log('here we are in action event and data is: ' + data);
  });
});

使用localhost:8080作为url而不是127.0.0.1:8080

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