【发布时间】:2017-11-17 15:16:46
【问题描述】:
我正在尝试将“howler”包含到 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 Error 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 服务器。
是否可以通过套接字 io 服务器包含外部脚本?
【问题讨论】:
标签: javascript html http socket.io client