Thursday, April 26, 2012
Saturday, April 21, 2012
javascript: String to XML
a quick utility function to convert from string to xml object.
Reference:
http://www.w3schools.com/xml/xml_dom.asp
Useful properties/methods for XML Object:
Reference:
http://www.w3schools.com/xml/xml_dom.asp
function stringToXML(text)
{
var xmldoc;
if (window.DOMParser)
{
var parser=new DOMParser();
xmldoc=parser.parseFromString(text,"text/xml");
}
else // Internet Explorer
{
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async=false;
xmldoc.loadXML(text);
}
return xmldoc;
}
Useful properties/methods for XML Object:
- getElementsByTagName(tagName)
- childNodes[index]
- nodeValue
- getAttribute(atttributeName)
- setAttribute(name, value)
- createElement(name)
- createTextNode(text)
- appendChild(element)
- removeChild(element)
Friday, April 13, 2012
WPF Controls in XNA
http://www.c-sharpcorner.com/uploadfile/iersoy/windows-controls-and-wpf-usercontrols-inside-an-xna-game-project/
http://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms
But the textbox is still not receiving input.
Alternative is to render a separate window
Have to Add Reference:
System.Xaml
WindowsFormsIntegration
System.Windows.Forms
PresentationCore
PresentationFramework
import
using System.Windows;
using System.Windows.Interop;
using System.Windows.Forms.Integration;
Run the program and you will get an error about STAThread
so add "[STAThread]" in eg.:
Program.cs
just before "static void Main(String[] args)"
http://stackoverflow.com/questions/835878/wpf-textbox-not-accepting-input-when-in-elementhost-in-window-forms
But the textbox is still not receiving input.
Alternative is to render a separate window
Window window = new Window();
window.Content = control;
WindowInteropHelper helper = new WindowInteropHelper(window);
helper.Owner = Window.Handle;
ElementHost.EnableModelessKeyboardInterop(window);
window.Show();
Have to Add Reference:
System.Xaml
WindowsFormsIntegration
System.Windows.Forms
PresentationCore
PresentationFramework
import
using System.Windows;
using System.Windows.Interop;
using System.Windows.Forms.Integration;
Run the program and you will get an error about STAThread
so add "[STAThread]" in eg.:
Program.cs
just before "static void Main(String[] args)"
Thursday, April 05, 2012
Sunday, April 01, 2012
HTML5 WebSocket server using c#
Use Socket.Listen(10) as per normal.
on Socket.Accept(): the HTML5 client from the browser will initiate a handshake.
grab the request using Socket.Receive(byte[])
C#:
Example:
"--- Request ---
GET /echo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:9998
Origin: null
Sec-WebSocket-Key: NBX4xUnlu1QWVPtGE2RFyw==
Sec-WebSocket-Version: 13"
To generate a response back, need a few steps.
Reference: http://en.wikipedia.org/wiki/WebSocket#WebSocket_protocol_handshake
Get the key, eg.: "NBX4xUnlu1QWVPtGE2RFyw=="
append the magic string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to the key.
Use SHA1 to hash the combined string.
Convert this string to base64
Reference: http://www.gamedev.net/topic/611744-websocket-c-handshake-client-does-not-response-on-handshake/
C#:
so in this case, accept key is "YOBURURfF9giWA8womjw1NpT9fk="
Then form the response.
Example:
"--- Response ---
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: YOBURURfF9giWA8womjw1NpT9fk="
use "\r\n" to end each line. and add an additional "\r\n" to the last line, ie., two "\r\n"
send this thru the socket.
example:
To decode any message from client. using the WebSocket.send(message) method
Reference: http://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side
sequence: "one byte which contains the type of data
one byte which contains the length
either two or eight additional bytes if the length did not fit in the second byte
four bytes which are the masks (= decoding keys)
the actual data"
to decode: "decodedByte = encodedByte XOR masks[encodedByteIndex MOD 4]"
C#:
on Socket.Accept(): the HTML5 client from the browser will initiate a handshake.
grab the request using Socket.Receive(byte[])
C#:
bytes = new byte[1024];
int bytesRec = client.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
Example:
"--- Request ---
GET /echo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:9998
Origin: null
Sec-WebSocket-Key: NBX4xUnlu1QWVPtGE2RFyw==
Sec-WebSocket-Version: 13"
To generate a response back, need a few steps.
Reference: http://en.wikipedia.org/wiki/WebSocket#WebSocket_protocol_handshake
Get the key, eg.: "NBX4xUnlu1QWVPtGE2RFyw=="
append the magic string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to the key.
Use SHA1 to hash the combined string.
Convert this string to base64
Reference: http://www.gamedev.net/topic/611744-websocket-c-handshake-client-does-not-response-on-handshake/
C#:
string combined = key + magicString;
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(combined));
string acceptKey = Convert.ToBase64String(hash);
so in this case, accept key is "YOBURURfF9giWA8womjw1NpT9fk="
Then form the response.
Example:
"--- Response ---
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: YOBURURfF9giWA8womjw1NpT9fk="
use "\r\n" to end each line. and add an additional "\r\n" to the last line, ie., two "\r\n"
send this thru the socket.
example:
byte[] outputBytes = Encoding.ASCII.GetBytes(output);
int result = client.Send(outputBytes);
To decode any message from client. using the WebSocket.send(message) method
Reference: http://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side
sequence: "one byte which contains the type of data
one byte which contains the length
either two or eight additional bytes if the length did not fit in the second byte
four bytes which are the masks (= decoding keys)
the actual data"
to decode: "decodedByte = encodedByte XOR masks[encodedByteIndex MOD 4]"
C#:
bytes = new byte[1024];
int bytesRec = client.Receive(bytes);
int second = bytes[1] & 127; // AND 0111 1111
int maskIndex = 2;
if (second < 126)
{
// length fit in second byte
maskIndex = 2;
}
else if (second == 126)
{
// next 2 bytes contain length
maskIndex = 4;
}
else if (second == 127)
{
// next 8 bytes contain length
maskIndex = 10;
}
// get mask
byte[] mask = { bytes[maskIndex],
bytes[maskIndex+1],
bytes[maskIndex+2],
bytes[maskIndex+3]};
int contentIndex = maskIndex + 4;
// decode
byte[] decoded = new byte[bytesRec - contentIndex];
for (int i = contentIndex, k = 0; i < bytesRec; i++, k++ )
{
// decoded = byte XOR mask
decoded[k] = (byte)(bytes[i] ^ mask[k % 4]);
}
data = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
Console.WriteLine(data);
Subscribe to:
Posts (Atom)