来自 WebStorm 的带有 Express 4.15 模板的 socket.io 不起作用 [英] socket.io with Express 4.15 template from WebStorm not working
问题描述
我正在尝试使用 socket.io
设置一个 Node.js
服务器.我正在使用 WebStorm
模板提供的最新模板.
I'm trying to setup a Node.js
server with socket.io
. I'm using the latest template given by WebStorm
template.
我的步骤是首先将 socket.io 添加到我的语言和框架 - Node.js 和 NPM.这是我的设置
My step is first add socket.io to my language and framework - Node.js and NPM. Here is my setting
然后在我的bin/www"中添加以下内容
Then in my "bin/www" I add the below
var io = require('socket.io')(server)
并将其添加到 bin/www 文件的底部(在服务器开始侦听后)
And add this to bottom of the bin/www file (after server has start listening)
io.on("connection",(socket)=>{
console.log("user connected" + socket.id)
})
然后我修改我的 index.jade 如下
Then I modify my index.jade as below
extends layout
block content
h1= title
p Welcome to #{title}
li what the ul1
li what the ul2
script.
src="/socket.io/socket.io.js"
script.
var socket = io();
运行服务器.访问页面,这就是我得到的
Run the server. visit the page and this is what I get
我收到错误
Uncaught ReferenceError: io is not defined
at localhost/:1
有人知道我的实现有什么问题吗?
Anyone know what wrong with my implementation?
编辑 1:我可以通过浏览器访问 socket.io.js
Edit 1: I can get to the socket.io.js by my browser
推荐答案
我猜您的浏览器找不到该库.您可以通过添加来包含它
I guess your browser can't find the library. You could include it by adding
src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"
到您的文件.
这篇关于来自 WebStorm 的带有 Express 4.15 模板的 socket.io 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!