//show messgae private void DisplayMessage(Message msg) { try { IList <Option> options = msg.GetOptions(); Boolean ifObserve = false; string responseCoap; responseCoap = "HTTP/1.1 200 OK\r\n"; responseCoap += "Content-Type: text/html; charset=UTF-8\r\n\r\n"; //Judge if the resouce can be Observed foreach (Option o in options) { if (o.Type.Equals(OptionType.MaxAge)) { ifObserve = true; break; } } //if can be Observed if (ifObserve) { responseCoap += "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + "<html><head><title>Observe</title>" + "<script type=\"text/javascript\"> var ws; function connectSocketServer() {" + "if (document.getElementById('btnSend').value == \"Observe\"){" + " document.getElementById('messageBoard').innerHTML = \"* Connecting to server ..<br/>\"; document.getElementById('btnSend').value =\"Cancel\";" + " ws = new WebSocket('ws://59.64.38.126:912');" + " var script_el=document.createElement(\"script\"); script_el.type=\"text/javascript\";" + " script_el.src=window.location.href + \"?observehost\";" + " document.getElementById('messageBoard').appendChild(script_el);" + " ws.onmessage = function (evt) { document.getElementById('messageBoard').innerHTML+=\"# \" + evt.data + \"<br />\"; };" + " ws.onopen = function () { document.getElementById('messageBoard').innerHTML +=\"* Connection open<br/>\";};" + " ws.onclose = function (){ document.getElementById('messageBoard').innerHTML += \"* Connection closed<br/>\"; };" + "} else{ window.location.reload(); }" + "}</script>" + "</head><body><div id=\"messageBoard\">" + msg.PayloadString + "</div><br/><input type=\"button\" id=\"btnSend\" value=\"Observe\" onclick=\"connectSocketServer();\" /></body></html>"; } else { responseCoap += msg.PayloadString; } Byte[] payloadBytes = msg.Payload; if (MediaType.IsImage(msg.ContentType)) { // show image // doesn't work :( //webBrowserPayload.DocumentStream = new MemoryStream(payloadBytes); // save to a temp file File.WriteAllBytes(_tempImagePath, payloadBytes); String path = "file:///" + _tempImagePath.Replace('\\', '/'); responseCoap = String.Format("<html><head><title></title></head><body><img src=\"{0}\" alt=\"\"></body></html>", path); } byte[] bytes = Encoding.UTF8.GetBytes(responseCoap); this.clientSocket.Send(bytes); this.clientSocket.Disconnect(false); this.clientSocket.Dispose(); } catch (Exception e) { Console.WriteLine(e.Message); } }