Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
290 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...