0
如何從節點服務器數據發送到使用Socket.io如何使用SocketIO
服務器端PHP服務器(Apache)的連接的Node.js和PHP服務器:
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服務器,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);
});
});
你的服務器監聽端口'9090' ,而你的客戶正試圖連接端口'8080' –
在mozila控制檯上顯示一些錯誤:GET XHR http:// localhost:8080/socket.io/1/[HTTP/1.1 400 Bad Request 21ms] –