簡體   English   中英

發射兩個網絡攝像頭 stream 並使用 socket.io nodejs 聊天到特定房間

[英]emitting both webcam stream and chat to specific room using socket.io nodejs

I have successfully implemented streaming webcam and chat at the same time using node.js and socket.io, I have an emit.html page for whoever is initiating the stream, and a visualize.html page for the clients or recipient, and but I have challenge in attaching網絡攝像頭 stream 僅發送到管理員創建的房間,請問如何僅將攝像頭發送到特定房間。 這是我下面的服務器代碼

const express = require("express");
const app = new express();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const formatMessage = require("./utils/messages");
const {
   userJoin,
   getCurrentUser,
   userLeave,
   getRoomUsers,
 } = require("./utils/users");

 const port = process.env.PORT || 3000;

 app.use(express.static(__dirname + "/public"));

 const botName = "Admin";

 app.get("/", function (req, res) {
 res.redirect("index.html");
});

io.on("connection", function (socket) {
socket.on("stream", function (image) {
socket.broadcast.emit("stream", image);
});

//Join Room
socket.on("joinRoom", ({ username, room }) => {
const user = userJoin(socket.id, username, room);

socket.join(user.room);

// Welcome current user
socket.emit("message", formatMessage(botName, "Welcome to Class!"));

// Broadcast when a user connects
socket.broadcast
  .to(user.room)
  .emit(
    "message",
    formatMessage(botName, `${user.username} has joined the class`)
  );

  // Send users and room info
  io.to(user.room).emit("roomUsers", {
    room: user.room,
    users: getRoomUsers(user.room),
  });
});

// Listen for chatMessage
socket.on("chatMessage", (msg) => {
  const user = getCurrentUser(socket.id);

  io.to(user.room).emit("message", formatMessage(user.username, msg));
});

// Runs when client disconnects
socket.on("disconnect", () => {
const user = userLeave(socket.id);

if (user) {
  io.to(user.room).emit(
    "message",
    formatMessage(botName, `${user.username} has left the class`)
  );

  // Send users and room info
  io.to(user.room).emit("roomUsers", {
    room: user.room,
    users: getRoomUsers(user.room),
  });
 }
});
});
http.listen(port, () => console.log(`Server running on port ${port}`));

請查看 git 存儲庫以獲取完整項目,我不介意進行任何調整以使其更好。 https://github.com/timotech/screen-cast謝謝

我能夠通過將 stream 套接字添加到 joinRoom 套接字來解決問題 這是代碼的調整:

socket.on("joinRoom", ({ username, room }) => {
const user = userJoin(socket.id, username, room);

socket.join(user.room);

//Display webcam to user
socket.on("stream", function (image) {
  socket.broadcast.to(user.room).emit("stream", image);
});
…

現在一切都按預期工作。 但我現在面臨的唯一挑戰是我將它托管在 Heroku 上,但它沒有按預期工作。

這是 Heroku 托管的鏈接。 對於教師: https://screencastst.herokuapp.com/對於學生訪問: https://screencastst.herokuapp.com/students.ZFC35FDC70D5FC69D269883A822C7AE

老師是唯一可以使用攝像頭的人,這就是為什么老師有不同的鏈接,學生只能看到正在教他們 class 的老師,這也是鏈接不同的原因但是聊天不起作用,這就是我的挑戰現在有。 但在本地主機上完美運行謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM