I tried running the following code as an unpacked chrome extension and got the following output in my console:
clicked
sent undefined
Error listening: net::ERR_SOCKET_NOT_CONNECTED
I also use separate software to create a UDP server on port 11111 and get nothing.
manifest.json
{
"name": "UDP Test",
"description": "Testing UDP connection.",
"version": "0.1",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"minimum_chrome_version": "33",
"sockets": {
"udp": {
"send": "*"
}
}
}
background.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('window.html', {
'bounds': {
'width': 400,
'height': 500
}
});
});
window.html
<html>
<head>
<button id="test">Run</button>
<script src="network.js"></script>
</head>
<body>
</body>
</html>
network.js
test.addEventListener('click', function() {
console.log("clicked");
chrome.sockets.udp.create({}, function (socketInfo) {
var socketId = socketInfo.socketId;
var arrayBuffer = stringToArrayBuffer("hello");
chrome.sockets.udp.send(socketId, stringToArrayBuffer("hello"), "127.0.0.1", 11111, function(sendInfo) {
console.log("sent " + sendInfo.bytesSent);
if (sendInfo.resultCode < 0) {
console.log("Error listening: " + chrome.runtime.lastError.message);
}
});
});
});
function stringToArrayBuffer(string) {
var buffer = new ArrayBuffer(string.length * 2);
var bufferView = new Uint16Array(buffer);
for (var i = 0, stringLength = string.length; i < stringLength; i++) {
bufferView = string.charCodeAt(i);
}
return buffer;
}
Any ideas why this is happening? I found some examples of udp chrome, but they all used experimental APIs, not that.
source
share