[英]Is there a way to trigger the socket.io connection event from NodeJS?
I'm trying some things with socket.io on NodeJS and I can't figure out how to trigger the the socket (only) from NodeJS. 我正在尝试在NodeJS上使用socket.io做一些事情,但我不知道如何(仅)从NodeJS触发套接字。
Till now I was using socket.io by calling it from the front end but I wonder if is it possible to do the same thing I did on the front end but this time on the nodeJS part(server side). 到目前为止,我是通过从前端调用socket.io来使用它的,但是我想知道是否可以做与前端相同的操作,但是这次是在nodeJS部分(服务器端)执行的。
My guess is it's not possible because is required a kind of connection(I like to call it a TCP connection,but I'm not sure if that's true or not) and without a second participant in the connection the socket won't work.That's my guess. 我的猜测是不可能的,因为需要一种连接(我喜欢将其称为TCP连接,但是我不确定这是否正确),并且在连接中没有第二个参与者的情况下,套接字将无法工作。那是我的猜测。
So what I'm doing now is : 所以我现在正在做的是:
app.js(server file) app.js(服务器文件)
...
const ioLib = require('./path/io.js')(io);
...
...
...
path/io.js(socket file) 路径/io.js(套接字文件)
module.exports = function(io){
io.on('connection', async function(socket) {
console.log('socket talks : a user connected');
...
...
});
module.exports.io = io;
}
And from an file.ejs file I do : 从file.ejs文件中,我做了:
var socket = io("url");
So with this,let's call it schema,I do the following : 因此,我们将其称为模式,我将执行以下操作:
When I access that webpage the 'connection' event is triggered in the sockets. 当我访问该网页时,套接字中会触发“连接”事件。 My question is,and I'm trying to formulate it as simple as I can : How can I do the same but without a webpage?Is it possible to trigger the sockets inside the NodeJS?
我的问题是,我想尽可能地简化它:如何在没有网页的情况下执行同样的操作?是否可以触发NodeJS内的套接字?
What do you think? 你怎么看?
It is possible to connect a Socket.IO server from a stand alone Node.js application (does not really matter where it runs) rather than a web front-end, accessed via a web browser. 可以通过独立的Node.js应用程序连接Socket.IO服务器(在运行位置并不重要),而不是通过Web浏览器访问的Web前端。 In order to achieve this, you should use
socket.io-client
. 为了实现这一点,您应该使用
socket.io-client
。 An example client usage might be as follows: 客户端用法示例如下:
// Node.js app: client.js
const io = require('socket.io-client');
const socket = io.connect('http://SERVER_IP:SERVER_PORT', {
reconnect: true
});
socket.on('connect', function (socket) {
console.log('Connected to the server!');
});
socket.emit('connected', 'Hi from the client side!');
In this case, your server side application should include something as follows: 在这种情况下,您的服务器端应用程序应包括以下内容:
// Node.js app: server.js
io.on('connection', function(socket) {
console.log('socket talks: a user connected');
// Print the message that comes from the socket client
socket.on('connected', function (msg) {
console.log(msg);
});
});
As you can see above, fundamentally, the architecture remains the same as server-client. 从上面可以看到,从根本上说,该体系结构与服务器-客户端相同。 Now, let's go one step further and put all those codes in a single js file, and see how it works:
现在,让我们更进一步,将所有这些代码放在一个js文件中,看看它是如何工作的:
// server/client together: crazy-socketapp.js
const io_server = require('socket.io').listen(3030);
io_server.sockets.on('connection', function (socket) {
console.log('A client is connected!');
socket.on('connected', function (msg) {
console.log(msg);
});
});
const io_client = require('socket.io-client');
const socket = io_client.connect('http://localhost:3030', {
reconnect: true
});
socket.on('connect', function (socket) {
console.log('Connected to the server!');
});
socket.emit('connected', 'Hi from the client side! ');
The output of the app: 应用程序的输出:
> A client is connected! > Connected to the server! > Hi from the client side!
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.