websocket读取服务器文件,用canvas绘制动态图片,图片通过websocket从服务器获取

简单的说就是想从服务器获取文件画到canvas上,然后会报错Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided. 有大大可以告诉我原因么,我就想测试一下,画个图出来

贴上代码:

canvas

var url = undefined;

var wsUri ="ws://134.91.11.156:8679";

canvas = document.getElementById('myCanvas');

ctx = canvas.getContext('2d');

img= new Image();

function init() {

output = document.getElementById("output");

testWebSocket();

}

function testWebSocket() {

websocket = new WebSocket(wsUri);

websocket.binaryType = "blob";

websocket.onopen = function(evt) {

onOpen(evt)

};

websocket.onclose = function(evt) {

onClose(evt)

};

websocket.onmessage = function(evt) {

onMessage(evt)

};

websocket.onerror = function(evt) {

onError(evt)

};

}

function onOpen(evt) {

writeToScreen("CONNECTED to:"+wsUri);

console.log("Websocket CONNECTED to " + wsUri);

doSend('{"type": "InitRenderer", "req": { "rid": 1, "sid": 0, "params": { "protocol": "tcp.ws", "rendertype": "GridLeapingRenderer", "fileid": "UVFData@./WholeBody-SCANLINE-132-lz4.uvf", "ioendpoint": "tcp.prefixed:134.91.11.156:6678", "streamingparams": { "xres": 1024, "yres": 768 } } } }');// is the command right?

}// if it send message successful, show measage on the screen

function onClose(evt) {

writeToScreen("DISCONNECTED");

console.log("Websocket DISCONNECTED");

}// show close message if the connent is over

function onMessage(evt) {

writeToScreen('RESPONSE: '+evt.data +'');// get the response and show it on screen,using "evt.data" is right? Can I direct show the JSON on the screen?

if(url != undefined)

URL.revokeObjectURL(url);

img.onload = function() {

console.log("drawing pic come on come on");

ctx.drawImage(img, 0, 0);

}

console.log("replacing image source");

url = URL.createObjectURL(evt.data); //Create DOMString ?? I think maybe something wrong here...

console.log(url);// can not get the url, i dont know why

img.src = url; // if i can get the url, I can draw the pic

websocket.close();

}

function onError(evt) {

writeToScreen('ERROR: '+ evt.data);

}// if sending is failed,show the message

function doSend(message) {

writeToScreen("SENT: " + message);

websocket.send(message);

} // send the command to the server

function writeToScreen(message) {

var pre = document.createElement("p");

pre.style.wordWrap = "break-word";

pre.innerHTML = message;

output.appendChild(pre);

} // write the message on the screen

WebSocket Test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值