English 中文(简体)
页: 1 io-我可在一片书状中提一下。
原标题:socket.io - I can t use socket.emit() in a socket.on()

我正在学习使用节点和袖珍。 我正在问一问一题。 目前,我正在着手开展一个纸张-剪切游戏,我有两位听众,负责我的议事日程:创建Room和加入Room。 在我和罗马诺的听众中,我谨向会议厅的两名成员寄发一份书状。 然而,当我坐在我的桌旁时(加入Room),我不能寄出一席。 这里有些法典可能有助于你更好地理解我的问题:

// server-side
io.on( connection , (socket) => {
  socket.emit( startGame ); // Works
  socket.on( joinRoom , (data) => {
    socket.emit( startGame ); // Does not work
    socket.to(data.room).emit( startGame ); // Does not work
    io.to(data.room).emit( startGame ); // Does not work
    io.emit( startGame ); // Works but it sends the socket to all clients and that s not what I want
  });
});
  // client-side
  useEffect(() => {    
    socket.on( startGame , (data) => {
      alert(socket.id);
    });
    
    return () => {
      socket.off( startGame );
    };
  }, [socket]);
// server.js
const express = require( express )
const app = express();
const http = require( http );
const { Server } = require( socket.io );
const cors = require("cors");
app.use(cors());
const gameController = require( ./controller/gameController );
const server = http.createServer(app);
const io = new Server(server, {
  cors: {
    origin: "http://localhost:3000",
  },
});

io.on( connection , (socket) => gameController.socketHandler(io, socket));

server.listen(3001);
module.exports = io;

我已经尝试通过研究其他例子来改变我的准则。 我接着发言。 我甚至修改了这封信的原文。 io,但无所作为。

问题回答

从Node.js开始的Im开始,我不知道,但我已经利用我的影响力,在发送地址之外的客户方面处理我的费用(我不知道我是否清楚)。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签