【问题标题】:how i can fix this socketio probelm我如何解决这个 socketio 问题
【发布时间】:2020-07-30 11:42:04
【问题描述】:

结果:

Fatal error:
  Uncaught UnexpectedValueException: The server returned an unexpected value.
  Expected "HTTP/1.1 101", had "HTTP/1.1 400"
  in C:\xampp\htdocs\vendor\wisembly\elephant.io\src\Engine\SocketIO\Version1X.php:266
Stack trace:
  #0 C:\xampp\htdocs\vendor\wisembly\elephant.io\src\Engine\SocketIO\Version1X.php(73): ElephantIO\Engine\SocketIO\Version1X->upgradeTransport()
  #1 C:\xampp\htdocs\vendor\wisembly\elephant.io\src\Client.php(60): ElephantIO\Engine\SocketIO\Version1X->connect()
  #2 C:\xampp\htdocs\emit.php(12): ElephantIO\Client->initialize()
  #3 {main} thrown in C:\xampp\htdocs\vendor\wisembly\elephant.io\src\Engine\SocketIO\Version1X.php on line 266

server.js:

const socket = require('socket.io'),
      express = require('express'),
      https = require('https'),
      http = require('http'),
      logger = require('winston');

const app = express();

const http_server = http.createServer(app).listen(3001);

logger.info('server running port 3001');

function emitNewOrder(http_server) {
    var io = socket.listen(http_server);

    io.sockets.on('connection',function (socket) {

        socket.on('new_order', function (data) {
            io.emit("new_order", data);
        });

    });
}

emitNewOrder(http_server);

emit.php:

<?php

include ("vendor/autoload.php");

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version1X;

$version = new Version1X("http://localhost:3001");

$client = new Client($version);

$client->initialize();

$client->emit("new_order", ["test1"=>"test1","test2"=>"test2"]);

$client->close();

【问题讨论】:

    标签: php node.js socket.io


    【解决方案1】:

    改用 Version2X

    <?php
    
    include ("vendor/autoload.php");
    
    use ElephantIO\Client;
    use ElephantIO\Engine\SocketIO\Version2X;
    
    $client = new Client(new Version2X('http://localhost:3001'));
    
    $client->initialize();
    $client->emit("new_order", ["test1"=>"test1","test2"=>"test2"]);
    $client->close();
    
    

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2019-10-14
      • 2020-03-25
      • 2020-02-14
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多