【问题标题】:How to connect Node.js and PHP server using SocketIO如何使用 SocketIO 连接 Node.js 和 PHP 服务器
【发布时间】:2017-06-04 01:16:53
【问题描述】:

如何使用 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);
  });
});

【问题讨论】:

  • 您的服务器正在侦听端口9090,而您的客户端正在尝试连接端口8080
  • 在 mozila 控制台上显示一些错误:GET XHR localhost:8080/socket.io/1 [HTTP/1.1 400 Bad Request 21ms]

标签: php node.js apache server socket.io


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 2019-07-14
    • 1970-01-01
    • 2012-02-08
    • 2012-07-22
    • 1970-01-01
    • 2018-12-15
    相关资源
    最近更新 更多