如何使用Socket.io
将数据从节点服务器发送到PHP服务器(Apache)服务器端:
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!');
客户端(PHP SERVER,Javascript代码):
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);
});
});
答案 0 :(得分:0)
使用localhost:8080作为url而不是127.0.0.1:8080