如何使用EJS,节点和Express连接到套接字?

时间:2018-09-24 02:55:38

标签: javascript node.js sockets express socket.io

我正在尝试使用socket.io,passport.js,ejs和express构建一个聊天应用程序。我的身份验证工作正常,但由于某种原因,我的套接字连接无法正常工作。这是我的index.js文件。套接字连接在底部处理。我关注了socket.io文档。

const app = require('express')();
const authRoutes = require('./routes/auth-routes');
const profileRoutes = require('./routes/profile-routes');
const passportSetup = require('./config/passport-setup');
const mongoose = require('mongoose');
const keys = require('./config/keys');
const cookieSession = require('cookie-session');
const passport = require('passport');
const passSocket = require('passport.socketio');
const server = require('http').Server(app);
const io = require('socket.io')(server);

//set up view engine
app.set('view engine', 'ejs');

app.use(cookieSession({
    maxAge: 24 * 60 * 60 * 1000,
    keys: [keys.session.cookieKey]
}));

//initialize passport
app.use(passport.initialize());
app.use(passport.session());

//set up routes
app.use('/auth', authRoutes);
app.use('/profile', profileRoutes);

//create home route
app.get('/', (req, res) => {
    res.render('home', {user: req.user});
});

app.get('/rooms', (req, res) => {
    //list existing rooms
});

app.post('/rooms', (req, res) => {
    //create a new chatroom
});

app.get('/room/{roomID}/subscribe', (req, res) => {
    //subscribe to this room
});

app.get('/room/{roomID}/messages', (req, res) => {
    //list messages of this room
});

app.post('/room/{roomID}/messages', (req, res) => {
    //post a message to this room
});

app.listen(3000, () => {
    console.log('App now listening for requests on port 3000');
});

server.listen(8080, () =>{
    console.log('Waiting for socket connection on port 8080...');
});

// Connect to mongodb
mongoose.connect(keys.mongodb.dbURI, { useNewUrlParser: true }, () => {
    console.log('Connected to MongoDB');
});

io.on('connection', (socket) => {
    console.log('Socket initialized...');
    socket.emit('prof', {name: 'jon'});
    socket.on('other', (data) => {
        console.log(data);
    })
});

,这是profile.ejs文件。这是验证用户身份时加载的页面。我什至尝试将脚本src切换到socket.io cdn,但这仍然无法正常工作。

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <title>Profile</title>
    <style>
        body{
            font-family: arial;
            margin: 0;
        }
        nav{
            background: #ff5353;
            padding: 20px 10px;
        }
        nav ul{
            max-width: 960px;
            margin: 0 auto;
            padding: 0;
        }
        nav li{
            list-style-type: none;
            display: inline-block;
            margin: 0 10px 0;
        }
        nav a{
            color: #fff;
            font-size: 18px;
            background: rgba(255,255,255,0.2);
            text-decoration: none;
            padding: 10px;
            display: block;
        }
        main,header{
            max-width: 960px;
            margin: 30px auto;
            padding: 0 10px;
            color: #333;
        }
    </style>
    </head>
    <body>
        <nav>
            <ul>
                <% if (user) { %>
                <li><a href="/auth/logout">Logout</a></li>
                <% } else { %>
                <li><a href="/auth/login">Login</a></li>
                <% } %>
                <li><a href="/">Homepage</a></li>
                <li><a href="/profile">Profile</a></li>
            </ul>
        </nav>
        <header>
            <h1>Welcome back to Smartchat, <%= user.username%></h1>
        </header>
        <main>
        </main>
    </body>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('https://127.0.0.1:8080');
        if(socket != undefined){
            console.log('Connected to socket...');
        }
        socket.on('prof', (data) => {
            console.log(data);
            socket.emit('other', {socket: 'connected'});
        })
    </script>
</html>

这是我第一次使用websockets,因此将不胜感激。如何连接到插座?

0 个答案:

没有答案
相关问题