首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >uWebSockets -将事件推送到服务器

uWebSockets -将事件推送到服务器
EN

Stack Overflow用户
提问于 2017-02-19 14:44:27
回答 1查看 2.4K关注 0票数 0

我有一个uWebSockets服务器,因为它似乎比socket.io服务器的性能友好得多。因此,我有一个服务器,它连接得很好,在遇到一些问题后,我让index.html客户端连接,但现在我不能从客户端向服务器推送事件。我做错了什么?

代码语言:javascript
复制
var WebSocketServer = require('uws').Server,
    express         = require('express'),
    path            = require('path'),
    app             = express(),
    server          = require('http').createServer(),
    createEngine    = require('node-twig').createEngine;

var wss = new WebSocketServer({server: server});

wss.on('connection', function (ws) {

    ws.on('join', function (value) {
        console.log('SOMEONE JUST JOINED');
    });

    ws.on('close', function () {
        //console.log('stopping client interval');
        clearInterval(id);
    });
});

server.on('request', app);

server.listen(8080, function () {
    console.log('Listening on http://localhost:8080');
});

index.html

代码语言:javascript
复制
<script>
      var host = window.document.location.host.replace(/:.*/, '');
      var server = new WebSocket('ws://' + host + ':8080');
      server.onmessage = function (event) {
        updateStats(JSON.parse(event.data));
      };

      server.onopen = function (event) {
        server.send("Here's some text that the server is urgently awaiting!");
        server.send('join');
      };

      function something() {
        console.log('WORKED');
        server.send('join');
      }

    </script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-20 06:06:32

您没有在服务器端设置事件侦听器来接收消息并对其做出反应。喜欢

代码语言:javascript
复制
ws.on('message', function (msg) {
    // Do something with the message received from the client
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42324454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档