如何通过按下按钮连接到节点服务器?

德瓦德尔弗里

我可以通过按下按钮连接到带有 socket.io 的 nodejs 服务器吗?例如file:///home...site/index.html我得到了我的页面和在我的本地机器上运行的服务器localhost:8080当我使用 xmlhttprequest 或其他方式调用函数时,我可以从我的文件连接到服务器吗?如何?有链接/教程吗?

埃德温·托雷斯

我在 GitHub 上有一个非常简单的 socket.io 示例:socketio-example将本示例中的 index.html 页面更新为如下所示:

<!doctype html>
<html>
    <head>
        <script src='/socket.io/socket.io.js'></script>
        <script>
          var socket;
          function makeConnection() {
            socket = io();
            socket.on('welcome', function(data) {
              addMessage(data.message);
              // Respond with a message including this clients' id sent from the server
              socket.emit('i am client', {data: 'foo!', id: data.id});
            });
            socket.on('polo', function(data) {
              addMessage(data.message);
            });
            alert('connected.');
          }

          function addMessage(message) {
            var text = document.createTextNode(message),
                el = document.createElement('li'),
                messages = document.getElementById('messages');
            el.appendChild(text);
            messages.appendChild(el);
          }

          function marco() {
            socket.emit('marco');
          }
        </script>
    </head>
    <body>
        <button onclick="makeConnection()">Connect</button>
        <button onclick="marco()">Marco!</button>
        <ul id='messages'></ul>
    </body>
</html>

这将在用户单击Connect时建立 socket.io 连接然后你可以点击Marco!发送消息并接收 Polo!回复。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过节点服务器连接到MongoDB的警告

如何通过Docker连接到本地MySQL服务器?

如何通过 swi prolog 连接到 MongoDB 服务器?

如何通过Swift连接到本地Firebase服务器?

如何通过cURL连接到OpenVPN服务器?

如何通过 Firebase 功能连接到本地服务器

如何通过FTP连接到FTP服务器

如何通过代理服务器连接到VPN

如何通过R连接到远程服务器?

如何从OSX通过SSH连接到Ubuntu服务器?

通过代理连接到服务器

如何使用UnboundID SDK通过SSL服务器证书连接到LDAP服务器?

在没有Internet的情况下通过VPN连接的服务器连接到Internet

Elasticsearch :通过连接到远程服务器。SSH(节点JS)

如何通过SSH连接通过远程服务器连接到Internet

使用 net nodejs 模块通过单击按钮连接到套接字服务器

连接到节点服务器自动查找掩码

如何使用相同的TLS会话通过数据连接连接到FTPS服务器?

通过 pdo 连接到服务器不起作用,如何向连接添加端口?

如何在连接到节点上的 websocket 服务器时传递消息

React Native Socket.io如何从设备连接到本地节点服务器

如何使用socket.io从节点连接到telnet服务器

通过android模拟器连接到服务器

通过OpenVPN使用NordVPN服务器连接到VPN服务器后,如何允许SSH进入终端?

so_reuseport下如何连接到特定的grpc服务器进程

如何在没有 rdp 的情况下连接到 AWS Windows 服务器?

无法通过PXE连接到服务器

通过Telnet连接到POP / SMTP服务器

通过OpenVPN连接到服务器LAN