C++ Websocket Introduction with Boost library Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] actually i want to create a content which which actually includes an http server websocket server playing with the whole power of the beast library bruce library also but this can be added with um complex for for the beginners let's say but so then i want to create a simple websocket server application with the synchron one in this content so i will also show you some tools how we can use to test our application uh we can sniff the websocket packages the uh you know tcp packages whatever we will send or get yeah this will be quite simple um content something kind of you know part one then the second one and the following ones let's say there will be other tutorials and which are still about a websocket using beast also i will show you how you can create really powerful applications web applications using the c plus plus this is really important and funny that i just found oh that is a thing right now but okay then let's start so before start um there are a bunch of information about this websocket protocol you can read here this is the number rpc 64 five-part websocket protocol implementation actually the explanation here how it should be implemented simply you know the generic protocol so if you want you can read good luck then there are a lot of informations that i didn't read more uh all of them actually so it's not necessary for the story but for information the the development package we will install actually i already installed it but i will show you which development package you should install simply grab lip boost all there let's check okay single oh this should be double o then i have this one but it depends on your version of the ubuntu yacht so this version can be different so you can also simply search with the sudo also rpg search boost yup this one i'm using right now you can simply install with an app install the post although but i am not doing this right now i already installed it so that is all for the package the beast also is installed with the leap boost all the i mean if you do this you installed every packages the auto packages that actually if probably you don't you don't need all of them but the beast also came with this package okay beast is the web socket implementation web socket part of the boost in the boost actually simply beast is the part of the boost okay then let's go to the visual studio i created a simple example website cpp here and we are starting right now to coding simply we will actually before start okay when you install this all dev package you installed out of header files the templates actually right not cpp files the this is this is actually the reason of this um the boost libraries are header only files because because they are templates right so we can check here also somewhere i just put here in the templates for example yeah it's also written here the definition of template must be visible right so that means there is no definition in the cpp file there is no cpp file actually all the definitions because of the template situation template structure all the definitions should be inside the header file so that is why which is why template libraries typically provide all template definitions in the header most boost libraries are header only yeah that is the thing that is why you will have a lot of header files when you install this development packages so there is no binary there is no cpp file okay so let's um include the necessary header files it's a good thing here um beast encapsulated all the boost actually sio header files that's why then we include boost beast and core and this is for core and also the left socket we need here i will not include another as i o header because it's already i think is included in this headers that is why no need okay but we will use the sio uh for socket applications in the next okay i think we will i need a stream and string to print something and threats to run multiple clients right okay so simply i'm just writing stretch so that is why it will be really simple so first we need an ip address right um i will use the loopback ip address here i'm in the virtual machine that i will show you also in the next how we can connect this ip address using some of the tool let's look and client tool this will be the website server right this is a simple one which is the synchron web socket server actually so simply address um here okay let's not let's first time not use the namespaces to understand then i will add the namespaces later simply boost zio yeah we see it and nets node and iep address i think yeah ip make address this one nice i remember zero zero one this is a loopback address right so and also the port number i need here this should be simple um then integer right actually constant port we should static cast and std sorry to change to integer to string i put something like that but doesn't matter you can put whatever you want right now you're in the local environment that'll be no problem so now we will create and we need actually i o context ir context calls on an operating system to perform the connection operation the necessary operation for example it doesn't matter for socket application for um serial application it doesn't matter whatever you want to do in the azio you need an i o context simply just say but okay that's not okay it's cool let's let's continue and again boost sio should be exist directly ircontext ioc i will run only one thread this is the thread number here all right let's see it concurrency suggestion to implementation on how many trades yeah this is the number of threads that i just put here the one uh if you are in the virtual machine don't uh put something none you know much number here because uh it can crash your environment so to protest okay just put one then you can increase later then we will simply create an acceptor for the tcp right for socket application and for this simply boost sio the namespace to access ip and in the tcp acceptor yeah this one except so we will pass okay wait this is not show me whether as i remember it should be uh ifc should be passed for first an argument yeah then it should be the address and port number like this address and port number okay then yeah we need a while loop here i will create two while loops it's not a good idea actually for my simple applications okay infinite for a while then we will wait a socket connection here right in the while then we will and throw a thread here and the attached thread after this so simply okay let me let me um create some name space here for the tcp namespace actually let's just using tcp maybe to a better idea boost xio ip tcp exactly then anymore boost as the iptcp we can use boost as well as a tcp i can remove this one and i can put here tcp socket now i am ready to use socket here socket that needs i context ioc that we created before and we need to accept it right acceptance acceptor accept accept this one and socket we will pass simply so when uh we need and when we get the new socket uh opening some request right we will wait here until here then we will accept socket so we can test for example here socket accepted something like i don't know compile it simply i will use a command line here i will give the path the libraries the headers where our headers exist the direct path within minus i it is in the usr include boost and which is the name example let's look at cpp let's try yeah there are some errors let's check let's check oops this is forget it sorry let's compile again actually we will get another error let's compile again yeah this is yeah this one here right now we are here this is related with uh p thread library so we need to call when we try to compile it we need to call the p thread like this if you don't need it if you don't have a p thread you should install it just simply search p direct that will be easy so let's compile again okay it's compiled nice but uh i will give some name websockets let's say to the binary okay now we have actually we have an example that we can run right web sockets okay there's a problem that's so it will consume the length of cpu power uh actually it's waiting here right now there's no problem so let's check for example let's open a new terminal we can simply connect from here right you can create a tcp connection socket connection here you can test it 80 80 30 right no okay i forget to put an end line here it's compiled again run it compile yeah as you see it uh when we try to open a circle connection right as a client then socket upset it simply but it asks out of time so there is no response right so that is the problem but yeah we will handle this don't worry so let's stop it okay let's continue and so i can accept it here let's keep it that way then after this of course we will create another threads here right we will attach it simply sd thread i'm using here and the normal example in the in the internet in the boost uh website you can find with the function it's called a function they called inside function but another function but uh i'm using the here in a different way so we will play a little bit i will use probably that you will see right now the lambda expression right i will not use any other function to keep it simple the example simple simply that edge like this it will create another uh you know thread right it will not block the main application that means we can connect with a number of clients okay nice then simply using the lambda expression yeah like this and here we should move the socket here we cannot copy we cannot pass a reference directly socket right this one socket here so that is why um i will create something like sku std using mu simple socket okay but it is yeah this should be a constant anymore whenever you capture something with the lambda then this should be constant so there are a few ways right now for this example we can we can put here a table to update it right then i will show another way for this okay it's correct okay let's try to compile until now to see there's any problem yeah there's no problem nice so let's continue then here here we should create a stream websocket stream that means sockets beast actually right beast web socket no no it is not right i cannot see it up so get three i cannot see it i don't know why because of the we're just chatting or do i something wrong p is 12 so i get right b oil is correct then tcp socket here we need this will be uh actually stream you can find here a lot of functions this is the stream that i will focus to accept functions and a few functions i will talk about with you the stream the set option and the end of the video probably you can read a whole here a synchronous read a synchrony right that this example will not use any of them actually this will be not a synchron application so we will just write and read right that is here should be a write and read whatever so let's pack websockets then um we'll simply right now this is not constant q we can use directly q i think here but also of course we should still move it with the std move yeah i mean we cannot you know it doesn't matter even if we here move this uh socket to queue then otherwise we have to move again right it's not copyable let's compile and try to see it's correct or not yeah there are some problems scdbs sdbs an example okay i forget your boost sorry no it should work yeah i forget here boost let's try to compile to see is there any problem yeah there's a problem nice then continue then continue and now you can accept the websocket handshake right we also um change a little bit the first handshake for for um if you protocol actually the per message deflate header is called and i will show you actually and in the end of this if i not for if i don't if i'm not forget it but then we s accept simply to accept the websocket connection okay then here we will put the while another while up here we are instill a threat right one one nothing this one and we need here some kind of container buffer actually it's called in the beast there are a few buffers the different buffers that the buffer types here you can read well this in this simple example you can use one of them it's not necessary but i will use for example right now the flat buffer okay but i tried also the multi-buffer that you can use it also it doesn't matter but i i'm i will use it then a flat buffer i'm gonna use it then here's boost beast flat buffer here see buffer we can call like this then vs read buffer to hold the incoming message from the client and let's let's try to print it how to we can do it directly right now boost paste there are buffers to stream function that we can use directly change converted as a printable object right the string of kind kind of the character array of values buffer c data yeah this one and let's print simply um out let's say std and linus not rem i forget it and yeah that is all right now to to try it okay that's not the correct way let's compile again i hope there'll be no problem yeah then run the example so as a client tool i will use here in the chrome there's an extension it's called an extension i think right this um websocket smart web socket client chrome extension yeah you can find simply smart web sockets client that you can install it i installed before right so it simply gives you an ability to test your application simply it will it will work with your application of your test so simply this is uh let me a little bit make it bigger to read easily so this is ws right without a security right now so this is an unsecured connection that we will see with also the wireshark i will show you a few things so photos loopback ip address and the port number okay we should write like this then we can connect it yeah so i can accept it and it's connected right now it's nice it's working so let's try to send something let's send some string until this is print or not yes prince it's working this is the simple that's not finished of course it's not echo back anything right now we will also see the echo message here what we sent this client so we can uh see it's a simple idea actually for this uh web socket with the boost with beast especially um to tell a few things actually for the move table okay it's not a good idea to make multiple here if it's not a good idea actually for everything here inside this container we can also remove this right there are a few also notations which will be better like this one as i remember let's try to compile and see this one one of the notation and here then yeah there is no there is no um const cast number table so we have to write to use the constants we have to const cast the skew anymore right here as the move in the std move const chest tcp socket this is socket right tcp socket but uh what was the rtcp socket we already include the namespace using i mean and simply here we should the reference put the parentheses are important yep that is all let's try to compile it again no there are problems what is the problem here right yeah this should be constant guest let's compile again okay okay let's clean the screen okay there is no problem we can also use like this so be careful whatever you uh capture here right it will be const even if this circuit so also we have to move it it's not copyable it's not possible to copy it you can compile it but when you try to run the example you will get some um access variations kind of weird happening things i don't know what will happen we can also use like this okay in the c thread okay then let's let's try to echo back what we will get from the client simply to do that we can simply ws write function and directly we can type data here buffer data this is the usage [Music] for this flat buffer okay and okay does this thing actually let's try to compile again and run the example go to the clients connect it yeah so i can accept it and i think the web socket client is also accepted send it yeah it is echo back directly we can send whatever we want as you see i don't know whatever you typed it will be a callback and you can see here simply so this is the uh okay this is echo server simple one synchron one we can also let's try to connect with another one yeah you can also see [Music] connect with another client this is working right now nice but there's a problem so we didn't uh implemented this connection so when i disconnected when i click click this this will throw an error actually some kind of probably uh accessibility or some memory corruption let's try oh yeah drawing in essence this the thing is here this connection should be implemented as a try catch mechanism so simple implementation should be like in the track edge here but of course it should be here and okay this is a cd exception no this should be beast but the beast boost beast right bruce beast you can call the namespaces you can import the namespaces if you want but for this example um i'm just playing like this but you can find uh some examples that i wrote for this tutorial and below the video the link you can find simply don't worry about it boost beast what what i told you yeah system error system error this one constant reference or something but system error right that's s a and here s a of course um this should be a if condition which is not accurate boost beast website white boost beast uh that's edible long it's a good idea to call import namespaces but whatever and closed see here this is the close this connection then we can simply shoot break here right to to release the threat right to release the actual while loop for the break i mean then let me copy this here whatever actually we can simply put break here yep that is all and yeah okay come on let's put here what we will get also the code message will be the way to call to print actually let's use std online okay let's try to compile again let's see is there a problem and what is an acidic number what is that i forget the parenthesis i should i should prepare before the video next time the code maybe but it's much more you know the real writing that's okay yep sockets then try connect it disconnect it connect it disconnect it yeah it's okay right now it's working there's no um throwing anything see let's connect again and send something say something say some now this is the sum uh don't worry about this this is right with the smart web socket application say something something simply work say something disconnected connected nice it's working this is a simple client server actually website server application so um what we can do right now okay okay as i told you i don't know actually i couldn't remember did i told before that there should be a handshake in the websocket protocol we can change we can adapt this handshake because there is some there are some configuration in the handshake okay which is actually the cold um for the compression yeah this can be but this is called the per message deflate header the header one we can simply play with this header this is the you know get method and get and after the sketch we can play this strings here from the code so to do that um simply we will use this should be let me check i can remember to set option this should be at the set option set the per message deflate extension options i will simply right now put some text there okay but it will not affect anything as i understand but i mean you cannot see the effect here but just if you want you can search on the internet this will be uh right now there are some default numbers for example as a version uh it will print 266 for this we can check for example right now uh okay i will install before the wireshark here you can also install it but i will not show you here it's not the content of this video okay let's try to connect again disconnect it okay so this will loop back here you can to filter it for web socket you can simply type the web socket here okay it will filter the packages so you shouldn't see all the packages here then i'm using the loopback uh as an ipad address i didn't put any ipaddress right here just and i choose to look back and i gave the websocket okay this is some queries i will clean them yeah continue the saving and i'll connect oops yeah let's let me stop it these are the handshake right now between the client and our binary our server so this is this is switching protocols see here and there are some bunch of information see per message here it's written per message deflate here the protocol and also the response here the boost b is 266. this is the default string which came from here we can't play with it we can't change this we can change for example as an example this text here this switching protocols means actually is a status quo that is actually used as i understand uh for a server to indicate tcp connection that the tcp connection is about to be used for different protocol i mean if you want to use different there are a lot of protocols that you can you can check the header file for this and the beast there are a lot of different protocols that i don't know right now i'm just trying to show you the the way how you can update this header okay so let's go back again here before before here read i think or actually set option this should be before except here we will simply call set option but um this is this is not enough we will need a stream based decorator to decorate it websocket i'm not going deep for this stream bass no because i again forget the boost here the stream base decorator decorator okay why is that working boost because i will forget again the beast both beast web sockets and stream base decorator simply then we can put here another lambda expression to update the string yeah what a good notation i put this we will not capture anything as i remember boost beast socket response type reference response type response set yeah don't use like this okay just include to namespace import namespace in the beginning of this file and we can put here right now directly the stream for example again oh okay this is not enough i forget okay come on definition give me and there is a response type lambda overloaded there should be a a field server we have to call beast http field server field server but i cannot see here set a file fields the field value removing any other instances of the field first removes any values okay the field the name and the value okay we want to field the server we want to simply update this header here let's compile again okay let's run go to there again client and yeah we need again sniffing saving we are right now connect it yeah i got it but i got a little much also let's try to remove them and disconnect remove them connect again yep these are stop it and in the switching protocols header yeah here see this string we update it but because of course this there's no big problem right now but it's not a good idea i mean not the correct way i just try to show you the possibility of the updating this header for compression for a different protocol usage if you need okay okay i think that is all for this tutorial in a simple manner but let's try let's go to google and web sockets clients boost beast absolutely simple swinging this one yeah this go to there we can directly add the username spaces the better way let's try this example with our server all right let's copy this and open a new one here paste it web sockets client cpp one is another example but whatever this is the uh funny names and okay usage whatever whatever okay we can directly writing compiled let me add another oh i couldn't attach it here i have to attach the terminal and this one websocket example this should be web socket client and web socket client let's say that's a binary name client okay i hope that would be no problem okay nice and run the web sockets client um it's asking let's check here client echo so it is the server but we will put here an ip address right so the loopback ip address 0 0 1 and the port number is 8083 and string like this i think string some yeah it's great to open the socket with the websocket sends the data and close the connection right it's broken another another stream nice so that is that is all for this tutorial in the next one uh we will focus the asynchronous application but with more also the object-oriented way uh which is actually it's written on the internet that i will i will i will write i will code again this one within line by line with an explanation right as much as possible clear i don't know yeah um let me let me check if i forget something to tell you i don't know you can write some comment or whatever you want don't forget to subscribe see you again bye [Music] [Music] you
Info
Channel: Ulas Dikme
Views: 3,504
Rating: undefined out of 5
Keywords: C++, weboscket, boost, beast
Id: ZSefPfZqxpo
Channel Id: undefined
Length: 44min 56sec (2696 seconds)
Published: Sun May 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.