我想在我的项目中使用 web socket,即在 open-cart 1.5 中用于聊天应用程序,我已经安装了node和 socket.io 包,并且它在单独的文件夹中工作,但我想在我的 open-cart 项目中使用该聊天应用程序
我的 test.js 文件
var app = require('http').createServer(handler),
io = require('socket.io')(app, {
cors: {
origin: "http://localhost:3000/",
methods: ["GET", "POST"]
}
}),
fs = require('fs')
app.listen(3000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});