从客户端播放音频时,消息从socket.io收到 - 的node.js [英] Play Audio from client when message is recieved from socket.io - node.js

查看:320
本文介绍了从客户端播放音频时,消息从socket.io收到 - 的node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过一段时间解决这个问题,但还没有找到太多

I've searched for a while for a solution to this problem, but haven't found much.

我的目标是从UDP客户端收到一条消息,该服务器临危并转发到一个web客户端,播放音频的每个消息被收到的时间剪辑。然而,由于某种原因,不播放音频。如果我从我的目录打开网页直,音频可以播放,但如果我尝试,并通过本地主机失败访问它加载。有谁知道一个解决方案吗?

My goal is to recieve a message from a udp client, which the server recieves and forwards to a web client, which plays an audio clip each time a message is recieved. However, for some reason the audio will not play. If I open the page straight from my directory, the audio can be played, but if I try and access it through localhost it fails to load. Does anyone know of a solution?

下面是客户端的JavaScript。

Here is the client side javascript.

var mySound = new Audio('/public/audio/Bloom.mp3');
mySound.load();
var socket = io.connect('http://localhost');
socket.on('message', function(data){
    console.log(data);
    $('#content').text(data);
    mySound.play();
    //document.getElementById('audiotag1').play();
});

本页面由server.js服务,使用socket.io和前preSS使Node.js的文件。我不从我的console.log收到任何错误。
这里是server.js

This page is served by server.js, a node.js file using socket.io and express. I don't receive any errors from my console.log. Here is the server.js

var app = require('express')()
    , server = require('http').Server(app)
    , io =require('socket.io')(server)
    , dgram = require('dgram');

var httpPort = 1234;
var udpPort = 5000;

server.listen(httpPort);

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

app.get('/', function(request, response){
    var ipAddress = request.socket.remoteAddress;
    console.log("New express connection from: " + ipAddress);
    response.sendfile(__dirname + '/public/index.html');
});

var udpSocket = dgram.createSocket('udp4', function(msgBuffer){
    var jsonMessage = JSON.parse(msgBuffer);
    io.sockets.emit('message', JSON.stringify(jsonMessage));
});
udpSocket.bind(udpPort, '127.0.0.1');

您可以去这个链接,查看错误镀铬了。
http://postimg.org/image/xkv7a2kwb/

You can go to this link to see the error chrome has. http://postimg.org/image/xkv7a2kwb/

有没有人对如何解决这一问题的任何想法?

Does anyone have any ideas on how to fix this?

推荐答案

当你有一个静态文件不正确防爆preSS配置这个错误通常发生。

This error usually occurs when you have incorrect Express config for static files.

从您的客户端JavaScript我可以看到你存储在您的静态文件公共文件夹中。

From your client-side JavaScript I can see that you store your static files in public folder.

因此​​某处的服务器端,你应该有这样的事情:

So somewhere on the server-side you should have something like this:

app.use('/public', express.static('/path/to/public'));

做这样 HTTP://localhost/public/audio/Bloom.mp3 请求将送达/路径/要/公/音频/ Bloom.mp3 文件。

警告:

由于防爆preSS 4存在的没有更多的app.use(app.router)和所有路由的方法将在它们出现的顺序添加。这意味着,你必须把code的上述前行第一个 app.get(...) app.post(。 ..)

Since Express 4 there is "no more app.use(app.router)" and "All routing methods will be added in the order in which they appear". This means that you have to put the above line of code before first app.get(...), app.post(...), etc.

希望这些信息将帮助别人。

Hope this information will help someone.

这篇关于从客户端播放音频时,消息从socket.io收到 - 的node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆