【发布时间】:2017-02-19 20:00:09
【问题描述】:
我正在尝试使用 Laravel 5.3、Socket.IO 和 Simple-Peer 创建一个视频会议系统。我遵循了本教程:https://www.youtube.com/watch?v=ieBtXwHvoNk,一切正常,但现在我想自动化它。我希望当两个用户访问这种链接时:/vide-conference/some_video_conference_id,他们得到连接,所以视频会议可以开始。
据我所知,如果我希望对等点在连接之前向另一个点发送数据,我需要一个信令服务器。我将使用 Socket.IO。但是你能清楚地解释一下我怎样才能让 Laravel 5.3 的一切都正常工作吗?我在这里找到了一个示例(不使用 laravel):https://bl.ocks.org/adammw/d9bf021c395835427aa0,但它是在谈论服务器端和客户端代码,我不明白,因为我不知道将服务器端代码放在哪里,因为我只有我的 ressources/assets/js 中的一个 javascript 文件 (app.js) 使用 gulp 编译,因此浏览器可以读取它。
现在,我的 app.js 是:
var getUserMedia = require('getusermedia')
getUserMedia({ video: true, audio: false }, function (err, stream) {
if (err) return console.error(err)
var Peer = require('simple-peer')
var peer = new Peer({
initiator: location.hash === '#init',
trickle: false,
stream: stream
})
peer.on('signal', function (data) {
document.getElementById('yourId').value = JSON.stringify(data)
})
document.getElementById('connect').addEventListener('click', function () {
var otherId = JSON.parse(document.getElementById('otherId').value)
peer.signal(otherId)
})
document.getElementById('send').addEventListener('click', function () {
var yourMessage = document.getElementById('yourMessage').value
peer.send(yourMessage)
})
peer.on('data', function (data) {
document.getElementById('messages').textContent += data + '\n'
})
peer.on('stream', function (stream) {
var video = document.createElement('video')
document.body.appendChild(video)
video.src = window.URL.createObjectURL(stream)
video.play()
})
})
我的看法是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<label>Your ID:</label><br/>
<textarea id="yourId"></textarea><br/>
<label>Other ID:</label><br/>
<textarea id="otherId"></textarea>
<button id="connect">connect</button><br/>
<label>Enter Message:</label><br/>
<textarea id="yourMessage"></textarea>
<button id="send">send</button>
<pre id="messages"></pre>
<script src="bundle.js" charset="utf-8"></script>
</body>
</html>
我的 gulpfile 是:
elixir(function(mix) {
mix.sass('app.scss')
.webpack('app.js');
});
我是 laravel 和 nodejs 的初学者,请告诉我如何将 socket.IO 集成到我的实际设置中!
谢谢你,祝你有美好的一天!
更新:我尝试使用 socket.io-client,添加这些代码行:
var io = require("socket.io-client");
var client = io.connect("127.0.0.1:8080/LaravelAppFolder");
client.on('connect',function() {
client.emit("test","foo");
});
但我收到此错误:
polling-xhr.js?0757:250 GET http://127.0.0.1:8080/socket.io/?EIO=3&transport=polling&t=LUpVKNI 404 (Not Found)
我不知道怎么解决!
更新2:我搜索了2天如何解决这个错误,但我不能,我不明白。每个解决方案都谈论服务器端代码、http.listen 等。我已经说过我不明白这个服务器端代码是什么。 如果您有任何建议,将不胜感激!
更新 3:我终于明白这个服务器端代码是什么了。我公开创建了一个 nodejs 文件夹,并使用以下简单代码创建了一个 server.js 文件:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
const redis = require('redis');
const client = redis.createClient();
server.listen(8080);
console.log("Listening.....");
io.on('connection', function(socket){
console.log('a user connected');
});
然后我在 cmd(节点 server.js)中运行服务器,一切都适用于服务器端(它说“正在侦听.....”),但我仍然收到客户端的 404 错误,我真的不知道为什么它不起作用,因为服务器应该解决这个错误。 任何帮助表示赞赏!
【问题讨论】:
标签: javascript node.js laravel socket.io p2p