使两个插座连接相互通信

时间:2021-12-27 14:04:17

Here is the scenario...

这是场景......

I am working on an app I had an idea for, I'm building it in ember with an express backend. I am using the express-ws so I can run the ws websocket package inside express better. I was not able to get just ws to work with express.

我正在开发一个我有想法的应用程序,我正在使用快速后端构建它。我正在使用express-ws所以我可以在内部运行ws websocket包更好。我无法与快递合作。

My app will have two people connecting to two different url's that are socket connections, so that they can send and receive information to the server without the other getting it. At least that's the way I've come up in my mind to do it.

我的应用程序将有两个人连接到两个不同的URL是套接字连接,这样他们就可以向服务器发送和接收信息而无需另一个获取它。至少那是我想到的方式。

What I want is when one user does an interaction over the socket, for that socket to send a message to the other socket to perform an action and send it's information to the user connected on it.

我想要的是当一个用户通过套接字进行交互时,该套接字将消息发送到另一个套接字以执行操作并将其信息发送给连接在其上的用户。

I hope that makes sense. With express-ws here is what I have done so far which works at a basic level.

我希望这是有道理的。使用express-ws到目前为止,我已经完成了基本级别的工作。

var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

app.use(function (req, res, next) {
  console.log('middleware');
  req.testing = 'testing';
  return next();
});

app.get('/', function(req, res, next){
    console.log('browser connected');
  res.send('welcome to the api browser');
});

app.ws('/', function(ws, req) {
  console.log('socket connected');
  var object = {
    message: 'welcome to the socket api',
    time: Date.now().toString()
  }
  ws.send(JSON.stringify(object));
});

app.listen(1337);

I haven't made the other connection yet but for the time being it will be the same, but when the user on one connection sends a certain message to their socket, I want that socket to perform something and then pass some data to the other socket so it can send some information to it's user.

我还没有建立其他连接,但目前它将是相同的,但是当一个连接上的用户向其套接字发送某个消息时,我希望该套接字执行某些操作然后将一些数据传递给另一个套接字,所以它可以发送一些信息给它的用户。

1 个解决方案

#1


1  

This might give you an idea of how to store the references for later use:

这可能会让您了解如何存储引用以供以后使用:

var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// array to hold the connections
var openChannels = [];

app.use(function(req, res, next) {
  console.log('middleware');
  req.testing = 'testing';
  return next();
});

app.get('/', function(req, res, next) {
  console.log('browser connected');
  res.send('welcome to the api browser');
});

app.ws('/', function(ws, req) {
  console.log('socket connected');

  // store connection for later reference
  openChannels.push(ws);

  // @todo: remove from array on disconnect

  // set broadcast callback
  ws.onmessage = function(msg) {
    openChannels.forEach(function(index, item) {
      if (item !== ws) { // make sure we're not sending to ourselves
        item.send(msg);
      }
    });
  };

  var object = {
    message: 'welcome to the socket api',
    time: Date.now().toString()
  }
  ws.send(JSON.stringify(object));
});

app.listen(1337);

#1


1  

This might give you an idea of how to store the references for later use:

这可能会让您了解如何存储引用以供以后使用:

var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

// array to hold the connections
var openChannels = [];

app.use(function(req, res, next) {
  console.log('middleware');
  req.testing = 'testing';
  return next();
});

app.get('/', function(req, res, next) {
  console.log('browser connected');
  res.send('welcome to the api browser');
});

app.ws('/', function(ws, req) {
  console.log('socket connected');

  // store connection for later reference
  openChannels.push(ws);

  // @todo: remove from array on disconnect

  // set broadcast callback
  ws.onmessage = function(msg) {
    openChannels.forEach(function(index, item) {
      if (item !== ws) { // make sure we're not sending to ourselves
        item.send(msg);
      }
    });
  };

  var object = {
    message: 'welcome to the socket api',
    time: Date.now().toString()
  }
  ws.send(JSON.stringify(object));
});

app.listen(1337);