简体   繁体   English

没有 node.js 的 Socket.io 客户端

[英]Socket.io client without node.js

I would like to have a web page that connects (is a client) to socket.io/node.js backend, but the web page should not be provided through/started using node.js, but instead it should be a separate project without any node.js usage.我想要一个连接(是客户端)到 socket.io/node.js 后端的网页,但该网页不应该通过/开始使用 node.js 提供,而应该是一个单独的项目,没有任何 node.js 用法。

How can I do that?我怎样才能做到这一点?

Socket.io comes with two separate libraries, one for client usage and one for creating a server. Socket.io带有两个独立的库,一个用于客户端使用,另一个用于创建服务器。
The client library can be used both in a web browser and also in a Node.js app.客户端库既可以在 Web 浏览器中使用,也可以在 Node.js 应用程序中使用。
The server library needs Node.js.服务器库需要 Node.js。

So it's important to implement your Socket.IO server with Node.js and then connect to it with the client library.因此,重要的是使用 Node.js 实现 Socket.IO 服务器,然后使用客户端库连接到它。

In order to use Socket.IO client library in a web page you should simply include it in the page using a script tag and then connect to your sever:为了在网页中使用 Socket.IO 客户端库,您只需使用script标记将其包含在页面中,然后连接到您的服务器:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

Your HTML page can be provided using any technology you want;您可以使用任何您想要的技术来提供您的 HTML 页面; For example you can produce the HTML page using Django framework and Python as its backend.例如,您可以使用 Django 框架和 Python 作为其后端来生成 HTML 页面。 Or simply run a web server (eg Nginx) or even open the HTML page as a file in your browser of choice.或者简单地运行一个 Web 服务器(例如 Nginx),甚至在您选择的浏览器中将 HTML 页面作为文件打开。

It is not expressly required to use node on the server side.没有明确要求在服务器端使用节点。 As an example, Flask has as server-side library using python: https://flask-socketio.readthedocs.io/en/latest/例如,Flask 使用 python 作为服务器端库: https : //flask-socketio.readthedocs.io/en/latest/

socket.io also has a C server library, and I am sure there are a number of other languages that can use it. socket.io 也有一个 C 服务器库,我相信还有许多其他语言可以使用它。 The issue here is that all the node documentation uses the "/socket.io/socket.io.js"script URL which implies that node.js is supplying the file somehow but we would like to get that file to serve without using Node.这里的问题是所有节点文档都使用“/socket.io/socket.io.js”脚本 URL,这意味着 node.js以某种方式提供文件,但我们希望在不使用 Node 的情况下提供该文件。

In the flask example, it looks like they serve it using this script tag:在烧瓶示例中,看起来他们使用这个脚本标签来提供它:

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>

Depending on what your Server-side is using, they may have their own client-side implementation as well.根据您的服务器端使用的内容,它们可能也有自己的客户端实现。 I personally recommend reading the documentation for your socket.io implementation to see if they specify a client library.我个人建议阅读 socket.io 实现的文档,看看它们是否指定了客户端库。

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

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