首页 > 解决方案 > Php 的 Elephant.io 服务器错误(尝试建立与服务器的连接时发生错误)

问题描述

我有一个问题要问你,我在我的php 应用程序中使用了socket.io大象.io ,但我已经处理了将近 2 天的大象.io 得到的错误。是不是我做错了什么?以下是我得到的错误和代码。

我的错误 在此处输入图像描述

我的索引.PHP

require __DIR__ . '/vendor/autoload.php';

use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;

$client = new Client(new Version2X('http://localhost:9090'));
$client->initialize();
$client->emit('send-message', [
    'name' => 'john',
    'surname' => 'doe'
]);
$client->close();

我的应用程序.JS

const server = require('http').createServer();
const io = require('socket.io')(server,{
    cors: {
        origin: "*",
    }
});
io.on('connection',function (socket){
    console.log('connected');
   socket.on('send-message',function (data){
       console.log(data);
       io.emit('message',data);
   })
   socket.on('disconnect',function (){
       console.log('disconnected')
   })
})
server.listen(9090);

我的套接字.HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.2.0/socket.io.js" 
integrity="sha512-WL6WGKMPBiM9PnHRYIn5YEtq0Z8XP4fkVb4qy7PP4vhmYQErJ/dySyXuFIMDf1eEYCXCrQrMJfkNwKc9gsjTjA==" 
crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>
var socket = io('http://localhost:9090');
socket.on('message',function (data){
    console.log(data);
});
socket.emit('send-message',{
    "name":"johnHTML",
    "surname": "doeHTML"
});
</script>
</body>
</html>

标签: phpnode.jssocket.io

解决方案


我有一次这个错误。我像你一样搜索但找不到它。我尝试了许多不同的方法,但都没有奏效。作为最后的手段,我将“socket.io”库的版本更改为2.2.0并且它有效。你应该试试这个


推荐阅读