简体   繁体   English

NodeJS和Websockets

[英]NodeJS and Websockets

Today is my first time using NodeJS, and Socket.io more particularly. 今天是我第一次使用NodeJS,更特别是Socket.io。 I developed a website with a PHP framework and would like to add notification and chat features. 我开发了一个带有PHP框架的网站,并希望添加通知和聊天功能。 I'm following the tutorial on the Socket.io website trying to adapt it to my needs, but I have a few questions. 我正在关注Socket.io网站上的教程 ,试图根据我的需求调整它,但我有几个问题。

  • First, I see that they are creating an HTTP server. 首先,我看到他们正在创建一个HTTP服务器。 What I want is the website to keep working as usual, meaning HTTP requests to be handled the same way, but on top of that I would like a websocket to be created whenever a page is loaded. 我想要的是网站继续像往常一样工作,这意味着HTTP请求以相同的方式处理,但最重要的是我希望在加载页面时创建websocket。 So instead of 而不是

     var http = require('http').Server(app); http.listen(3000, function(){ console.log('listening on *:3000'); }); 

could we create a websocket server and use the WS protocol instead? 我们可以创建一个websocket服务器并使用WS协议吗? I've seen a few libraries online but I would like to know if there is a standard one or a better way to do it. 我在网上看过几个图书馆,但我想知道是否有标准版或更好的方法。

  • The other thing is I can't find the right path to the server-side Socket.io script. 另一件事是我找不到服务器端Socket.io脚本的正确路径。 Therefore the connection can't be established. 因此无法建立连接。 From I saw online, it should look like DOMAIN/socket.io:3000 . 从我在网上看到,它应该看起来像DOMAIN / socket.io:3000 Now I'm getting lost since it's my first time using Node: where can I find this "socket.io" file on the server? 现在我迷路了,因为这是我第一次使用Node:在哪里可以找到服务器上的“socket.io”文件? In my project folder, all I see is a node_modules folder (which does include a socket.io folder but I'm not sure how to use it). 在我的项目文件夹中,我看到的只是一个node_modules文件夹(它包含一个socket.io文件夹,但我不知道如何使用它)。

Please note that my script on the server runs without any errors, even though I haven't had a chance to establish a connection 请注意,即使我没有机会建立连接,我在服务器上运行的脚本也没有任何错误

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000, function(){
    console.log('listening on *:3000');
});

io.on('connection', function(socket){
    console.log('a user connected');
});

Here's the current code on the client side: 这是客户端的当前代码:

var socket = io('<?php echo base_url(); ?>:3000, {path: "/sockets/socket.io"}');

NodeJS is a webserver. NodeJS是一个网络服务器。 Unlike PHP, you can run multiple servers on different ports from one server. 与PHP不同,您可以在一台服务器的不同端口上运行多个服务器。 So this creates a webserver: 这样就创建了一个网络服务器:

var app = require('express')();
var http = require('http').Server(app);

http.get("/",(req,res)=>res.write("Hello World"));

http.listen(3000, function(){
console.log('listening on *:3000');
});

And this is the socket: 这是套接字:

var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('a user connected');
});

Note that Socket.io needs a webserver for their handshake,so an additional http server is neccessary. 请注意,Socket.io需要一个Web服务器来进行握手,因此需要额外的http服务器。 However, you dont need to use this server for serving pages, so you can do: 但是,您不需要使用此服务器来提供页面,因此您可以执行以下操作:

var io = require("socket.io")(8000);//creates a webserver on port 8000, no need to do more with the pure http server
io.on('connection', function(socket){
console.log('a user connected');
});

Unlike PHP you dont need to embed scripts, you can import them: 与PHP不同,您不需要嵌入脚本,您可以导入它们:

var so=require("socket.io");//works if socket.io is installed in modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM