我正在开发一个需要实时聊天的聊天应用程序项目,所以我在我的服务器端使用了用nodejs编写的socketio,而不是在我的主聊天应用程序react-native项目中使用了socketio-client。
但是现在出现了一个问题,我的套接字没有初始化。我无法将我的服务器与我的主应用程序连接。我正在使用 socketio 和 socketio 客户端,我的两个套接字版本都是相同的 4.5.1,但它甚至没有连接。我尝试使用旧版本的套接字,但它也不起作用,我也尝试将本地主机端口更改为 4000,但它也不起作用。
我的服务器代码:
const express = require('express');
var bodyParser = require('body-parser');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const port = process.env.PORT || 3000;
require('./src/config/database')
const user_routes = require('./src/user/users.routes');
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.json())
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.use('/User', user_routes)
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('send_message',(data)=>{
console.log("received message in server side",data)
io.emit('received_message',data)
})
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
server.listen(port, () => {
console.log( `Server running at http://localhost:${port}/`);
});
我的应用程序套接字服务文件代码:
import io from 'socket.io-client';
const SOCKET_URL = 'http://localhost:3000'
class WSService {
initializeSocket = async () => {
try {
this.socket = io(SOCKET_URL, {
transports: ['websocket']
})
console.log("initializing socket", this.socket)
this.socket.on('connect', (data) => {
console.log("=== socket connected ====")
})
this.socket.on('disconnect', (data) => {
console.log("=== socket disconnected ====")
})
this.socket.on('error', (data) => {
console.log("socekt error", data)
})
} catch (error) {
console.log("scoket is not inialized", error)
}
}
emit(event, data = {}) {
this.socket.emit(event, data)
}
on(event, cb) {
this.socket.on(event, cb)
}
removeListener(listenerName) {
this.socket.removeListener(listenerName)
}
}
const socketServcies = new WSService()
export default socketServcies
我标记的地方应该是已连接= true,但在开发控制台中它是错误的,我已经完成了控制台日志,因此检查它是否正在连接,我可以看到它没有连接。如何连接?
我的应用程序或服务器没有错误,我已经检查了很多次,并且当我运行我的应用程序时,我的服务器也在运行。
我假设您的前端和后端在本地主机中运行。 文档说如果前端与后端在同一个域中,则不需要使用URL。由于您已声明
options
参数,因此您可以首先使用默认参数 window.location
:
class WSService {
initializeSocket = async () => {
try {
this.socket = io(window.location, {
transports: ['websocket']
})
console.log("initializing socket", this.socket)
this.socket.on('connect', (data) => {
console.log("=== socket connected ====")
})
this.socket.on('disconnect', (data) => {
console.log("=== socket disconnected ====")
})
this.socket.on('error', (data) => {
console.log("socekt error", data)
})
} catch (error) {
console.log("scoket is not inialized", error)
}
}
emit(event, data = {}) {
this.socket.emit(event, data)
}
on(event, cb) {
this.socket.on(event, cb)
}
removeListener(listenerName) {
this.socket.removeListener(listenerName)
}
}
回答我自己的问题 问题是我正在使用 android 模拟器,而模拟器中的 android 无法连接到本地主机,您需要使用代理 ip,因此当我在
http://10.0.2.2:3000
中添加 const SOCKET_URL = 'http://10.0.2.2:3000'
时,它的工作正常
感谢 Gorbypark,他在不和谐中告诉我这件事
不要指定socket-io连接的主机/端口。它可以自己解决。
根据 documentation,如果没有将 URL 指定为参数,它会尝试连接到
window.location
。
所以而不是
this.socket = io(SOCKET_URL, {
transports: ['websocket']
})
就做吧
this.socket = io()
我不确定它是否适用于其他参数。你可以这样尝试
this.socket = io(undefined, {
transports: ['websocket']
})
以上问题有什么解决办法吗????