我正在尝试使用PHP和socket.io构建实时聊天应用程序。我无法连接到node.js端。
index.php
<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
var serverLocation = "http://sub.domain.com:3000";
var socket = io(serverLocation);
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
var port = process.env.PORT || 3000;
http.listen(port, function () {
console.log("Server listening at port %d", port);
});
io.on("connection", function (socket) {
console.log('test)
})
当我尝试使用应用程序时,它会返回此错误。
GET http://sub.domain.com:3000/socket.io/?EIO=3&transport=polling&t=Mkq7S0V net::ERR_CONNECTION_TIMED_OUT
我试图这样更改连接方法。
var socket = io(serverLocation, {
transports: ['websocket','polling'],
forceNew: true
});
然后出现新错误。
index.js:83 WebSocket connection to 'ws://sub.domain.com:3000/socket.io/?EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established.