我试图包括"咆哮"进入由socket i.o提供的http。对客户。因此,如果事件被触发并分发给客户端,他们会收到声音通知。
我尝试了来自https://socket.io/get-started/chat/的socket ios入门页面(聊天)的基本示例,并尝试将howler包含在这样的基础index.html中
<!doctype html>
<html>
<head>
<title>Socket.IO chat with sound at load</title>
</head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="./howler.js"></script>
<script>
var sound = new Howl({
src: ['sound.mp3']
});
sound.play();
</script>
</body>
</html>
但是我收到了一个404 not found for localhost:/howler.js
有道理。因为我似乎只通过套接字io提供一个文件:
var app = require('express')();
var http = require('http').Server(app);
var howler = require("howler");
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
console.log(__dirname);
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
我的第一个修复是通过/howler.js>为客户提供服务;但为此我必须运行另一个http服务器。
是否可以通过socket io服务器包含外部脚本?
答案 0 :(得分:2)
var app = require('express')();
var http = require('http').Server(app);
var howler = require("howler");
var io = require('socket.io')(http);
app.get('/howler.js', function(req, res){
res.sendFile(__dirname + '/howler.js');
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
console.log(__dirname);
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});