导出Socket.io模块以在全球范围内可用。(Export Socket.io module to be available globally.)
好吧我有一些要求,我希望能够在我的服务器的任何地方使用Socket.IO,特别是在执行任何CRUD操作时发送事件的路径中。
以下是一个名为sockets.js的文件,我用它来初始化我的套接字。
(function IIFE() { 2 'use strict'; 3 4 var socketIO = require('socket.io'); 5 6 exports.initialize = function (server) { 7 var io = socketIO.listen(server) 8 9 io.sockets.on('connection', (socket) => { 10 console.log(socket) 11 console.log("Connected"); 12 13 io.sockets.on('disconnect', (socket) => { 14 console.log('Disconnect') 15 }); 16 }); 17 } 18 19 })();
以下是我的app.js,它本质上是我的服务器。
2 (function IIFE() { 3 'use strict'; 4 5 const chalk = require('chalk'); 6 7 if(!process.env.NODE_ENV) { 8 console.log(chalk.red('No environment specified...') + chalk.blue('defaulting to \'development\'')); 9 process.env.NODE_ENV = 'development'; 10 } 11 12 const express = require('express'); 13 const mongoose = require('mongoose'); 14 15 const config = require('./config/environment'); 16 17 mongoose.connect(config.mongo.uri, config.mongo.options); 18 19 mongoose.connection.on('error', function(err) { 20 console.error('Mongo Error: ' + err); 21 }); 22 23 const app = express(); 24 const server = require('http').createServer(app); 25 + 26 var io = require('./sockets.js'); + 27 io.initialize(server); + 28 29 require('./config/express')(app); 30 require('./routes')(app); 31 32 server.listen(config.port, config.ip, function() { 33 console.log('order-api listening on %d in %s mode', config.port, app.get('env')); 34 }); 35 36 exports = module.exports = app; 37 })();
以下文件位于另一个名为api的文件夹中。 整个应用程序是一个功能正常的应用程序我试图将套接字集成到。 但是我似乎无法理解如何从我的sockets.js集成套接字并在以下路由处理程序中使用它们。
(function IIFE() { 2 'use strict'; 3 4 const router = require('express').Router(); 5 6 router.use('/carts', require('./cart')); 7 router.use('/orders', require('./order')); 8 router.use('/reports', require('./report')); 9 router.use('/validators', require('./validator')); 10 11 module.exports = router; 12 })();
Ok So I have some requirements, I would like to be able to use Socket.IO anywhere throughout my server, specifically within the routes to send out events when any CRUD operations have been performed.
The following is a file named sockets.js which I am using to initialize my sockets.
(function IIFE() { 2 'use strict'; 3 4 var socketIO = require('socket.io'); 5 6 exports.initialize = function (server) { 7 var io = socketIO.listen(server) 8 9 io.sockets.on('connection', (socket) => { 10 console.log(socket) 11 console.log("Connected"); 12 13 io.sockets.on('disconnect', (socket) => { 14 console.log('Disconnect') 15 }); 16 }); 17 } 18 19 })();
The following is my app.js which is essentially my server.
2 (function IIFE() { 3 'use strict'; 4 5 const chalk = require('chalk'); 6 7 if(!process.env.NODE_ENV) { 8 console.log(chalk.red('No environment specified...') + chalk.blue('defaulting to \'development\'')); 9 process.env.NODE_ENV = 'development'; 10 } 11 12 const express = require('express'); 13 const mongoose = require('mongoose'); 14 15 const config = require('./config/environment'); 16 17 mongoose.connect(config.mongo.uri, config.mongo.options); 18 19 mongoose.connection.on('error', function(err) { 20 console.error('Mongo Error: ' + err); 21 }); 22 23 const app = express(); 24 const server = require('http').createServer(app); 25 + 26 var io = require('./sockets.js'); + 27 io.initialize(server); + 28 29 require('./config/express')(app); 30 require('./routes')(app); 31 32 server.listen(config.port, config.ip, function() { 33 console.log('order-api listening on %d in %s mode', config.port, app.get('env')); 34 }); 35 36 exports = module.exports = app; 37 })();
The following file is in another folder called api. The entire application is a functioning application I am trying to integrate sockets into. However I cannot seem to understand how to Integrate sockets from my sockets.js and use them in the following route handlers.
(function IIFE() { 2 'use strict'; 3 4 const router = require('express').Router(); 5 6 router.use('/carts', require('./cart')); 7 router.use('/orders', require('./order')); 8 router.use('/reports', require('./report')); 9 router.use('/validators', require('./validator')); 10 11 module.exports = router; 12 })();
原文:https://stackoverflow.com/questions/42237632
最满意答案
我找到了问题的答案,感谢@MigratedPigeon,因为他让我想到了默认servlet的类。
tomcat服务器有一个默认的servlet,tomcats的默认servlet类是
org.apache.catalina.servlets.DefaultServlet
另一方面,Websphere没有默认的servlet,这就是我收到错误“无法找到Servlet名称”的原因。
在我在原始问题中链接的答案中,静态文件服务可以通过使用web-ext.xml文件由websphere激活,但仍然无法解决我的web.xml文件具有“默认”servlet的问题。
在我们的应用程序中,我们使用spring,所以最后我用spring dispatcher servlet替换了web.xml中的默认servlet,现在我的web.xml文件对tomcat和websphere都有效。
I found the answer to my problem, thanks to @MigratedPigeon because he got me thinking about the class of my default servlet.
A tomcat server has a default servlet, the class for tomcats default servlet is
org.apache.catalina.servlets.DefaultServlet
Websphere on the other hand does not have a default servlet, thats why I get the error "Servlet Name could not be found".
As in the answer I linked in the original question, static file serving can be activated by websphere by using the web-ext.xml file but that still did not solve the issue of my web.xml file having a "default" servlet.
In our application we use spring, so in the end I replaced the default servlet in web.xml with springs dispatcher servlet and now my web.xml file is valid for both, tomcat and websphere.
相关问答
更多-
如果我要用python制作一个小游戏,应该做什么准备工作?[2023-06-20]
e -
找到my.classpath.Servletname但已损坏:SRVE0227E:(my.classpath.Servletname was found, but is corrupted: SRVE0227E:)[2023-04-15]
问题只是因为当我使用1.7 JDK时,我正在使用1.8 JDK编译我的Web应用程序。 我一交换JDK就解决了问题。 错误消息告诉我,因为它非常具体,但没有提及有关类版本的任何内容。 The problem was simply because I was compiling my web application using a 1.8 JDK when I should have been using a 1.7 JDK. As soon as I swapped the JDK, the problem ... -
WebApplicationProviderImpl通过jersey-server.jar中的META-INF / services条目注册。 您是否有机会将泽西岛重新包装到您自己的罐子中而错过了META-INF /服务条目? 因此,要解决此异常,需要将来自jersey-core jersey-server和jersey-servlet的META-INF / services文件夹添加到apk的META-INF文件夹中。 然后您将无法使用Eclipse进行部署,但以下命令行将起作用: cd PROJECT_ ...
-
实际上,Bluemix状态页面正在报告2月5日开放的关于自由运行时SSO服务问题的通知。 请访问https://developer.ibm.com/bluemix/support/#status Actually the Bluemix status page is reporting a notification opened on Feb 5th about an issue of SSO service with the liberty runtime. Check on https://develo ...
-
如果查看Docker Hub上的“标签”选项卡,您将看到还有其他历史标签,包括websphere-liberty:8.5.5.9-webProfile7。 请注意,这些图像代表了时间快照,例如,在创建新版本的基础Ubuntu映像时不会重建它们。 目的是Liberty提供零迁移,因此您应该始终能够使用最新的。 你显然已经找到了反例...... If you look at the 'tags' tab on Docker Hub you will see that there are other histor ...
-
Eclipse e4应用程序无法在Java Web Start中启动(Eclipse e4 application does not start in Java Web start)[2023-10-26]
更改了jar中的manefest文件。 这是通过做jar -ufm 我添加的更改包括:权限:所有权限 Codebase: * Application-Name: <> Trusted-Only: true Trusted-Library: true Changed the manefest file inside the jar. This by doing jar -ufm The changes that I added are: Permissions: all-permissi ... -
在8.5.5.11之前,WebSphere Application Server随IBM Java SDK 6一起提供,作为默认的嵌入式Java SDK。 所以,你看到的是正确的。 从8.5.5.11开始,由于即将结束的Java SE 6服务,可以选择将IBM Java SDK 8作为嵌入式默认设置。 此外,IBM Java SDK 8始终可用作可选扩展,您可以在WAS安装之上安装它。 (对于Java SDK 7也是如此)。 所以,你有2个选项。 由于您正在从头开始迁移,因此从8.5.5.11(或更高版本) ...
-
这不是正常情况。 我也不希望你为简单的下载服务部署WAR。 我认为你的Tomcat实例是由其他东西控制的,例如一个IDE,比如Eclipse。 你也不应该那样做生产。 如果您单独运行Tomcat,则应解决问题。 That is not the normal case. I also don't expect that you're deploying a WAR for a simple download service. I think that your Tomcat instance is contr ...
-
你的.jsp文件名是什么? 输入index.jsp作为文件名,然后重试! What's your .jsp filename? Enter index.jsp as the file name and then try again!
-
由于SRVE0303E,WAS 8.5.5.9无法启动Web应用程序(WAS 8.5.5.9 cannot start webapplication because of SRVE0303E)[2023-07-16]
我找到了问题的答案,感谢@MigratedPigeon,因为他让我想到了默认servlet的类。 tomcat服务器有一个默认的servlet,tomcats的默认servlet类是 org.apache.catalina.servlets.DefaultServlet 另一方面,Websphere没有默认的servlet,这就是我收到错误“无法找到Servlet名称”的原因。 在我在原始问题中链接的答案中,静态文件服务可以通过使用web-ext.xml文件由websphere激活,但仍然无法解决我的web ...