我正在尝试为highlightjs
设置网络工作者。我无法在我的网络工程师中导入库。这是带有node.js服务器的角度应用程序。我确定有一些简单的愚蠢的东西我不知道。请帮我解决这个问题。
highlight.webworker.js
onmessage = function(event) {
console.log('worker location', self.location);
importScripts('highlight.pack.js'); // DOES NOT LOAD
var result = self.hljs.highlightAuto(event.data);
postMessage(result.value);
}
webworker.controller.js node.js webwoker控制器,它将脚本提供给客户端(需要这样做才能避免跨域错误)
// get highlight webwroker to client
exports.highlight = function(req, res) {
fs.readFile(__dirname + '/highlight.worker.js', 'utf8', function(err, data) {
if (err) return res.status(400).json({error: 'could not retrieve worker'})
return res.send(data);
});
};
http://locahost:9000/webworkers/highlight返回webworker脚本(如上所示)
webworker工厂
var app = angular.module('webworker.services', []);
app.factory('highlightWebworker', ['$q', 'wizMarkdownSvc', function($q, wizMarkdownSvc) {
var worker = new Worker('/webworkers/highlight');
var defer = $q.defer();
worker.onmessage = function(code) {
console.log('data from worker',code);
defer.resolve(code);
};
return {
highlightCode: function(markdownStr) {
defer = $q.defer();
worker.postMessage(markdownStr); // Send data to our worker.
return defer.promise;
}
};
}]);