I'm trying to connect socket.io with vuejs. Running 'node server' on the terminal does nothing and when I visit http://localhost:3000/ it gives Cannot GET /. Plus on the client side, I keep getting the error sockjs-node ERR_CONNECTION_TIMED_OUT
Server
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server,{
cors: {
origin: "http://localhost:8080",
methods: ["GET", "POST"]
}
})
io.on('connection', socket => {
socket.on('join-room', (roomId, userId) => {
socket.join(roomId)
socket.to(roomId).broadcast.emit('user-connected', userId)
socket.on('disconnect', () => {
socket.to(roomId).broadcast.emit('user-disconnected', userId)
})
})
})
server.listen(3000)
Vue
<script>
import io from "socket.io-client";
import Peer from "peerjs";
var socket = io.connect("http://localhost:3000");
export default {
created(){
const myPeer = new Peer(undefined, {
host: '/',
port: '3002'
})
myPeer.on('open', id => {
socket.emit('join-room', this.$route.params.id, id)
})
socket.on('user-connected', userId => {
console.log('user connected')
})
}
}
</script>